3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 1649 => ["firstName" => "jack", "lastName" => "straw"], 1650 => ["firstName" => "jack", "lastName" => "straw"], 1651 => ["firstName" => "jack", "lastName" => "straw"], 1652 => ["firstName" => "jack", "lastName" => "straw"], 1653 => ["firstName" => "jack1", "lastName" => "straw"], 1654 => ["firstName" => "jack1", "lastName" => "straw"] ]; foreach ($array as $key => &$row) { $compositeKey = $row['firstName'] . '-' . $row['firstName']; if (!isset($ref[$compositeKey])) { $ref[$compositeKey] = &$row; } else { $ref[$compositeKey]['mergedWith'][] = $key; unset($array[$key]); } } var_export($array);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
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:
90.6 ms | 1150 KiB | 4 Q