3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['spubid' => 'A00502','sfirst'=>'J.','smi'=>'A.','slast'=>'Doe'], ['spubid' => 'A00502','sfirst'=>'J.','smi'=>'F.','slast'=>'Kennedy'], ['spubid' => 'A00502','sfirst'=>'B.','smi'=>'F.','slast'=>'James'], ['spubid' => 'BXXXXX','sfirst'=>'B.','smi'=>'F.','slast'=>'James'], ['spubid' => 'A00502','sfirst'=>'S.','smi'=>'M.','slast'=>'Williamson'], ]; $result = []; foreach ($array as $row) { $id = $row['spubid']; if (!isset($ref[$id])) { $ref[$id] = $row; $counter[$id] = 0; $result[] = &$ref[$id]; } else { unset($row['spubid']); ++$counter[$id]; foreach ($row as $k => $v) { $ref[$id][$k . $counter[$id]] = $v; } } } var_export($result);

preferences:
40.83 ms | 404 KiB | 5 Q