<?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);
preferences:
24.7 ms | 405 KiB | 5 Q