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 git.master_jit, git.master
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', ), ), ), )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
69.63 ms | 407 KiB | 5 Q