3v4l.org

run code in 300+ PHP versions simultaneously
<?php $poorly_merged = [ (object)["id" => "5", "unit" => 1], (object)["id" => "3", "text" => "three"], (object)["id" => "1", "text" => "one"], (object)["id" => "2", "text" => "two"], (object)["id" => "4", "text" => "four"], (object)["id" => "5", "text" => "five"], (object)["id" => "3", "unit" => 0], (object)["id" => "1", "unit" => "0"], (object)["id" => "2", "unit" => 0], (object)["id" => "4", "unit" => "0"] ]; $output = []; foreach ($poorly_merged as $object) { if (!isset($output[$object->id])) { $output[$object->id] = $object; } else { foreach ($object as $property => $value) { $output[$object->id]->{$property} = $value; } } } ksort($output); // optionally order by ids var_export(array_values($output));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array ( 0 => (object) array( 'id' => '1', 'text' => 'one', 'unit' => '0', ), 1 => (object) array( 'id' => '2', 'text' => 'two', 'unit' => 0, ), 2 => (object) array( 'id' => '3', 'text' => 'three', 'unit' => 0, ), 3 => (object) array( 'id' => '4', 'text' => 'four', 'unit' => '0', ), 4 => (object) array( 'id' => '5', 'unit' => 1, 'text' => 'five', ), )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array ( 0 => (object) array( 'id' => '1', 'text' => 'one', 'unit' => '0', ), 1 => (object) array( 'id' => '2', 'text' => 'two', 'unit' => 0, ), 2 => (object) array( 'id' => '3', 'text' => 'three', 'unit' => 0, ), 3 => (object) array( 'id' => '4', 'text' => 'four', 'unit' => '0', ), 4 => (object) array( 'id' => '5', 'unit' => 1, 'text' => 'five', ), )
Output for 7.2.0 - 7.2.34
array ( 0 => stdClass::__set_state(array( 'id' => '1', 'text' => 'one', 'unit' => '0', )), 1 => stdClass::__set_state(array( 'id' => '2', 'text' => 'two', 'unit' => 0, )), 2 => stdClass::__set_state(array( 'id' => '3', 'text' => 'three', 'unit' => 0, )), 3 => stdClass::__set_state(array( 'id' => '4', 'text' => 'four', 'unit' => '0', )), 4 => stdClass::__set_state(array( 'id' => '5', 'unit' => 1, 'text' => 'five', )), )

preferences:
176.48 ms | 402 KiB | 191 Q