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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.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 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
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.1 ms | 410 KiB | 5 Q