3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 1650 => ["firstName" => "jack", "lastName" => "straw"], 1649 => ["firstName" => "jack", "lastName" => "straw"], 1651 => ["firstName" => "jack", "lastName" => "straw"], 1652 => ["firstName" => "jack", "lastName" => "straw"], 1653 => ["firstName" => "jack1", "lastName" => "straw"], 1654 => ["firstName" => "jack1", "lastName" => "straw"] ]; ksort($array); $temp = []; foreach ($array as $key => $row) { $compositeKey = $row['firstName'] . '-' . $row['firstName']; $temp[$compositeKey] ??= $row; $temp[$compositeKey]['mergedWith'][] = $key; } $result = []; foreach ($temp as $row) { $result[array_shift($row['mergedWith'])] = $row; } var_export($result);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array ( 1649 => array ( 'firstName' => 'jack', 'lastName' => 'straw', 'mergedWith' => array ( 0 => 1650, 1 => 1651, 2 => 1652, ), ), 1653 => array ( 'firstName' => 'jack1', 'lastName' => 'straw', 'mergedWith' => array ( 0 => 1654, ), ), )

preferences:
121.23 ms | 407 KiB | 5 Q