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 = count($myarray) - 2; $i >= 0; --$i) { // start from 2nd last subarray, and move toward the start $max = max(count($myarray[$i]), count($myarray[$i+1])); // make sure all elements are accounted for for($k = 0; $k < $max; ++$k) { // iterate and perform subtractions / declarations $myarray[$i][$k] = ($myarray[$i][$k] ?? 0) - ($myarray[$i+1][$k] ?? 0); // declare/overwrite new values as encountered } } var_export($myarray);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
array ( 0 => array ( 0 => 0, 1 => 46, 2 => -11, 3 => 0, ), 1 => array ( 0 => 0, 1 => 24, 2 => 30, 3 => 0, ), 2 => array ( 0 => 0, 1 => 0, 2 => -16, 3 => 0, ), 3 => array ( 0 => 0, 1 => 15, 2 => 27, 3 => 0, ), )

preferences:
146.72 ms | 408 KiB | 5 Q