<?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.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- 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,
),
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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:
179.13 ms | 402 KiB | 152 Q