How can I group data by key name?

Source: Internet
Author: User
How can I group data by key name?
$arr1 = array ('0' => array ('fid' => 1, 'tid' => 1 , 'name' =>'Name2' ),'2' => array ('fid' => 1, 'tid' => 1,  'name' =>'Name1' ),'1' => array ('fid' => 1, 'tid' => 5 , 'name' =>'Name3' ),'3' => array ('fid' => 1, 'tid' => 7 , 'name' =>'Name4' ),'4' => array ('sid' => 2, 'tid' => 9,  'name' =>'Name5' ),'5' => array ('cid' => 2, 'tid' => 9,  'name' =>'Name5' ));


Is there any fastest way to group data by key names fid, sid, and cid.


Reply to discussion (solution)

$arr1 = array ('0' => array ('fid' => 1, 'tid' => 1 , 'name' =>'Name2' ),'2' => array ('fid' => 1, 'tid' => 1,  'name' =>'Name1' ),'1' => array ('fid' => 1, 'tid' => 5 , 'name' =>'Name3' ),'3' => array ('fid' => 1, 'tid' => 7 , 'name' =>'Name4' ),'4' => array ('sid' => 2, 'tid' => 9,  'name' =>'Name5' ),'5' => array ('cid' => 2, 'tid' => 9,  'name' =>'Name5' ));foreach($arr1 as $v) {  $k = key($v);  $res[$k][] = $v;}print_r($res);
Array(    [fid] => Array        (            [0] => Array                (                    [fid] => 1                    [tid] => 1                    [name] => Name2                )            [1] => Array                (                    [fid] => 1                    [tid] => 1                    [name] => Name1                )            [2] => Array                (                    [fid] => 1                    [tid] => 5                    [name] => Name3                )            [3] => Array                (                    [fid] => 1                    [tid] => 7                    [name] => Name4                )        )    [sid] => Array        (            [0] => Array                (                    [sid] => 2                    [tid] => 9                    [name] => Name5                )        )    [cid] => Array        (            [0] => Array                (                    [cid] => 2                    [tid] => 9                    [name] => Name5                )        ))

Considering that the key names of the member arrays may be inconsistent, that is, the sid may be behind the name
You can
$ K = key ($ v );
Change
$ K = key (array_intersect_key (array ('fid' => '', 'Sid '=>'', 'CID' => ''), $ v ));

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.