3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ "id" => 96, "shipping_no" => "212755-1", "part_no" => "reterty", "description" => "tyrfyt", "packaging_type" => "PC" ], [ "id" => 96, "shipping_no" => "212755-1", "part_no" => "dftgtryh", "description" => "dfhgfyh", "packaging_type" => "PC" ], [ "id" => 97, "shipping_no" => "212755-2", "part_no" => "ZeoDark", "description" => "s%c%s%c%s", "packaging_type" => "PC" ] ]; function array_group_by($arr, array $keys) { if (!is_array($arr)) { trigger_error('array_group_by(): The first argument should be an array', E_USER_ERROR); } if (count($keys) == 0) { trigger_error('array_group_by(): The Second argument Array can not be empty', E_USER_ERROR); } // Load the new array, splitting by the target key $grouped = []; foreach ($arr as $value) { $grouped[$value[$keys[0]]][] = $value; } // Recursively build a nested grouping if more parameters are supplied // Each grouped array value is grouped according to the next sequential key if (count($keys) > 1) { foreach ($grouped as $key => $value) { $parms = array_merge([$value], [array_slice($keys, 1, count($keys))]); $grouped[$key] = call_user_func_array('array_group_by', $parms); } } return $grouped; } var_export( array_group_by($array, ['id', 'shipping_no']) );
Output for 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 ( 96 => array ( '212755-1' => array ( 0 => array ( 'id' => 96, 'shipping_no' => '212755-1', 'part_no' => 'reterty', 'description' => 'tyrfyt', 'packaging_type' => 'PC', ), 1 => array ( 'id' => 96, 'shipping_no' => '212755-1', 'part_no' => 'dftgtryh', 'description' => 'dfhgfyh', 'packaging_type' => 'PC', ), ), ), 97 => array ( '212755-2' => array ( 0 => array ( 'id' => 97, 'shipping_no' => '212755-2', 'part_no' => 'ZeoDark', 'description' => 's%c%s%c%s', 'packaging_type' => 'PC', ), ), ), )
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:
58.95 ms | 408 KiB | 5 Q