yu asked this 2 years ago

php , add previous sub-array value to to next sub-array value

<?php


$arr = [ [1,2,4], [4,2,6], [2,1,8] ];


// in each sub-array's end elemnent


//add previous sub-array's end-element

how to do with for iteration


?>


Noel 2 years ago

Here you go..,


<?php

$arr = [ [1,2,4], [4,2,6], [2,1,8] ];

$sum = 0;

foreach ($arr as $subarray) {

  $last_element;

  foreach ($subarray as $val) {

    $last_element = $val;

  }

  $sum = $sum + $last_element;

}

  echo $sum;

?>