3v4l.org

run code in 300+ PHP versions simultaneously
<?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));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, stdClass given in /in/JI5OK:28 Stack trace: #0 /in/JI5OK(41): array_unique_by_key(Array) #1 {main} thrown in /in/JI5OK on line 28
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/JI5OK on line 28 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 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
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.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
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.3.11, 4.4.0 - 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.

preferences:
325.17 ms | 401 KiB | 455 Q