3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $actualArray = array( user1 => array( 'name' => 'User1', 'age' => '25', 'lastLogin' => '2013-08-16' ), user1 => array( 'name' => 'User1', 'age' => '25', 'lastLogin' => '2013-08-10' ), user2 => array( 'name' => 'User2', 'age' => '35', 'lastLogin' => '2013-08-08' ), user1 => array( 'name' => 'User1', 'age' => '25', 'lastLogin' => '2013-07-10' ) ); function uniqueAssocArray($array, $uniqueKey) { if (!is_array($array)) { return array(); } $uniqueKeys = array(); foreach ($array as $key => $item) { if (!in_array($item[$uniqueKey], $uniqueKeys)) { $uniqueKeys[$item[$uniqueKey]] = $item; } } return $uniqueKeys; } var_dump(uniqueAssocArray($actualArray, 'name'));
Output for 5.5.0 - 5.6.21, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Notice: Use of undefined constant user1 - assumed 'user1' in /in/o63Ys on line 3 Notice: Use of undefined constant user1 - assumed 'user1' in /in/o63Ys on line 8 Notice: Use of undefined constant user2 - assumed 'user2' in /in/o63Ys on line 13 Notice: Use of undefined constant user1 - assumed 'user1' in /in/o63Ys on line 18 array(2) { ["User1"]=> array(3) { ["name"]=> string(5) "User1" ["age"]=> string(2) "25" ["lastLogin"]=> string(10) "2013-07-10" } ["User2"]=> array(3) { ["name"]=> string(5) "User2" ["age"]=> string(2) "35" ["lastLogin"]=> string(10) "2013-08-08" } }
Output for 4.3.0 - 5.4.45
array(2) { ["User1"]=> array(3) { ["name"]=> string(5) "User1" ["age"]=> string(2) "25" ["lastLogin"]=> string(10) "2013-07-10" } ["User2"]=> array(3) { ["name"]=> string(5) "User2" ["age"]=> string(2) "35" ["lastLogin"]=> string(10) "2013-08-08" } }