3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $array = array ('1' => (object)array( 'id' => 225, 'user_id' => 1, 'name' => 'Blue Quilted Leather Jacket by Minusey - $499' ), '2' => (object)array( 'id' => 222, 'user_id' => 1, 'name' => 'Darling New Bathtub by Duravit - $6300' ), '3' => (object)array( 'id' => 222, 'user_id' => 1, 'name' => 'Darling New Bathtub by Duravit - $6300' ) ); //print_r(array_column((array)$array, 'id')); function array_unique_by_key($array, $key = 'id') { $found = array(); // Encountered IDs $out = array(); // Output array foreach ($array as $value) { if (!array_key_exists($key, $value)) throw new Exception('Can\'t find key "' . $key . '"'); $id = $value[$key]; // If already encountered, skip if (in_array($id, $found)) continue; // Otherwise, add to found values and to output array $found[] = $id; $out[] = $value; } return $out; } print_r(array_unique_by_key($array));
based on h5qmX
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/JI5OK:29 Stack trace: #0 /in/JI5OK(41): array_unique_by_key(Array) #1 {main} thrown in /in/JI5OK on line 29
Process exited with code 255.
Output for 5.0.0 - 5.6.28
Fatal error: Cannot use object of type stdClass as array in /in/JI5OK on line 29
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/JI5OK on line 28
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_NEW in /in/JI5OK on line 28
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/JI5OK on line 28
Process exited with code 255.