3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'One', 'price' => 10 ], [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Two', 'price' => 15 ], [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Three', 'price' => 20 ], [ 'id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'One', 'price' => 25 ], [ 'id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'Two', 'price' => 40 ] ]; $result = []; foreach ($array as $row) { $companies = array_splice($row, -2); $result[$row['id']] ??= $row; $result[$row['id']]['companies'][] = $companies; } var_export($result);
Output for 8.0.1 - 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 ( 112 => array ( 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'companies' => array ( 0 => array ( 'company' => 'One', 'price' => 10, ), 1 => array ( 'company' => 'Two', 'price' => 15, ), 2 => array ( 'company' => 'Three', 'price' => 20, ), ), ), 224 => array ( 'id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'companies' => array ( 0 => array ( 'company' => 'One', 'price' => 25, ), 1 => array ( 'company' => 'Two', 'price' => 40, ), ), ), )
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.

preferences:
124.6 ms | 408 KiB | 5 Q