3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [1 => 2], [1 => 2], [2 => 1], [3 => 1], ]; $result = []; foreach ($array as $row) { $k = key($row); if (!isset($ref[$k])) { $ref[$k] = 0; $result[][$k] =& $ref[$k]; } $ref[$k] += $row[$k]; } var_export($result);

preferences:
30.77 ms | 405 KiB | 5 Q