3v4l.org

run code in 300+ PHP versions simultaneously
<?php $myarray = [ [0, 70, 19, 0], [0, 24, 14], // note missing 4th element [0, 15, 11, 0], [0, 15, 27, 0] ]; for ($i = sizeof($myarray)-2; $i>=0; --$i) { // start from 2nd last subarray, and move toward the start $max = max(sizeof($myarray[$i]), sizeof($myarray[$i+1])); // make sure all elements are accounted for for($k = 0; $k < $max; ++$k) { // iterate and perform subtractions / declarations $myarray[$i][$k] = (isset($myarray[$i][$k]) ? $myarray[$i][$k] : 0) - (isset($myarray[$i+1][$k]) ? $myarray[$i+1][$k] : 0); // declare/overwrite new values as encountered } } var_export($myarray);

preferences:
17.74 ms | 402 KiB | 5 Q