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; } print_r(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/I7U2u on line 3 Notice: Use of undefined constant user1 - assumed 'user1' in /in/I7U2u on line 8 Notice: Use of undefined constant user2 - assumed 'user2' in /in/I7U2u on line 13 Notice: Use of undefined constant user1 - assumed 'user1' in /in/I7U2u on line 18 Array ( [User1] => Array ( [name] => User1 [age] => 25 [lastLogin] => 2013-07-10 ) [User2] => Array ( [name] => User2 [age] => 35 [lastLogin] => 2013-08-08 ) )
Output for 4.3.0 - 5.4.45
Array ( [User1] => Array ( [name] => User1 [age] => 25 [lastLogin] => 2013-07-10 ) [User2] => Array ( [name] => User2 [age] => 35 [lastLogin] => 2013-08-08 ) )