<?php
$arr = [ [1,2,4], [4,2,6], [2,1,8] ];
// in each sub-array's end value
//add previous sub-array's end-value
$arr_count = count($arr);
$sub_arr_count = count($arr[0]);
for($i = 0; $i < $arr_count; $i++){
//( $i < 1 ) ? continue; : 'no iteration';
//if($i<1){continue;}
for($j = 0; $j < $sub_arr_count; $j++){
$arr[$i][2] = $arr[$i-1][2] + $arr[$i][2] ;
}
}
?>
fails and gives errors ,
why this error is ?
plz help
PHP Notice: Undefined offset: -1 in HelloWorld.php on line 20
PHP Notice: Trying to access array offset on value of type null in HelloWorld.php on line 20
PHP Notice: Undefined offset: -1 in /runtime/php/3ydr8gcjt/HelloWorld.php on line 20
PHP Notice: Trying to access array offset on value of type null in /runtime/php/3ydr8gcjt/HelloWorld.php on line 20
PHP Notice: Undefined offset: -1 in /runtime/php/3ydr8gcjt/HelloWorld.php on line 20
PHP Notice: Trying to access array offset on value of type null in /runtime/php/3ydr8gcjt/HelloWorld.php on line 20
this is input array ,
$arr = [ [1,2,4], [4,2,6], [2,1,10] ];
and this is output array ,
$arr = [ [1,2,4], [4,2,10], [2,1,18] ];
objective :
first sub-array will not change,
every next-sub-array will change.
and each next-subarray's end value is sum of previous-sub-array's end value.
this to be accomplished with a for consruct.
you get -1 index because your $i loop starts from 0, but then you access index $i - 1 in the array which is -1. Start your $i loop from 1.
Your $j loop is completely unnecessary, you need to change only the last item, not all in the row.