PHP groups two-dimensional arrays according to key

Source: Internet
Author: User
We often get a two-dimensional array, we will find that the result and we want some deviation, may need to be based on a two-dimensional array of a field in the group grouping.

First look at the following array,

Array ([0] = = Array ([id] = 1[wo_id] = 2[evaluate_id] = 1[type] + Group 1[ctime] = 2016-12-02 11:39:34) [1  ] = = Array ([id] = 2[wo_id] = 31[evaluate_id] = 2[type] + Group 1[ctime] = 2016-12-12 11:39:50) [2] = = Array ([id] = 3[wo_id] = 31[evaluate_id] = 4[type] = Group 2[ctime] = 2016-12-02 11:40:01) [3] = = Array ( [ID] [4[wo_id] = 2[evaluate_id] = 5[type] + Group 2[ctime] = 2016-12-07 10:21:40) [4] = = Array ([id] =&G T  5[WO_ID] [= 2[evaluate_id] = 3[type] = Group 2[ctime] = = 2016-12-07 10:21:51) [5] = = Array ([id] + 6[wo_id] = 2[evaluate_id] = 2[type] + Group 3[ctime] = 2016-12-07 10:21:56) [6] = = Array ([id] = 7[wo_id] + 4 [evaluate_id] = 2[type] + group 3[ctime] (2016-12-07-10:22:10))

Here is a very typical example, if I want the data in the array to be separated according to the type (grouping), if not grouped, may be very redundant loop, so the following a grouping function:

public static function Array_group_by ($arr, $key) {$grouped = [];foreach ($arr as $value) {$grouped [$value [$key]][] = $valu e;} Recursively build a nested grouping if more parameters be supplied//each grouped array value was grouped according to The next sequential Keyif (Func_num_args () > 2) {$args = Func_get_args (); foreach ($grouped as $key = = $value) {$parm s = Array_merge ([$value], Array_slice ($args, 2, Func_num_args ())); $grouped [$key] = Call_user_func_array (' Array_group_ By ', $parms);}} return $grouped;}

$arr: two-dimensional array

$key: Key that needs to be grouped

The resulting results are:

Array ([group 2] = = Array ([0] = = Array ([id] = 1[wo_id] = 2[evaluate_id] = 1[ctime] = 2016-12-02 11:39:34[ Type] = + Group 2) [1] = = Array ([id] = 2[wo_id] = 31[evaluate_id] = 2[ctime] = 2016-12-12 11:39:50[type] = > Group 2) [2] = = Array ([id] = 3[wo_id] = 31[evaluate_id] = 4[ctime] = 2016-12-02 11:40:01[type] = Group 2)) [Group 1] = = Array ([0] = = Array ([id] = 4[wo_id] = 2[evaluate_id] = 5[ctime] = 2016-12-07 10:21:40[typ  E] + = group 1) [1] = = Array ([id] = 5[wo_id] [2[evaluate_id] = 3[ctime] = 2016-12-07 10:21:51[type] = Group 1)))
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.