Php array loop

Source: Internet
Author: User
Php array loop
$ Arr = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 1 => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), 2 => array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 4 => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), 5 => array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5 ),);


The original array is shown above and you want to obtain the following results in a loop:
$ Rs = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 'group _ 1' => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ),), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 'group _ 2' => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5 ),));


The group can be extracted and sorted at the same time. I can't write it myself. please help me.


Reply to discussion (solution)

$ Arr = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 1 => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), 2 => array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 4 => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), 5 => array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5 ),); foreach ($ arr as $ k => $ v) {if ($ v ['group'] = 0) {$ g = $ k; $ res [$ g] = $ v;} else $ res [$ g] ['group _ 1'] [] = $ v;} print_r ($ res );
Array ([0] => Array ([group] => 0 [name] => guest test1 [sort] => 0 [group_1] => Array ([0] => array ([group] => 1 [name] => guest test2 [sort] => 1) [1] => Array ([group] => 1 [name] => guest test3 [sort] => 2 ))) [3] => Array ([group] => 0 [name] => guest test4 [sort] => 3 [group_1] => Array ([0] => Array ([group] => 2 [name] => guest test5 [sort] => 4) [1] => Array ([group] => 2 [name] => guest test6 [sort] => 5 ))))

$ Arr = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 1 => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), 2 => array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 4 => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), 5 => array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5),); $ data = array (); foreach ($ arr as $ key => $ val) {if ($ val ['group'] = 0) {$ tmp = $ key; $ data [$ tmp] = $ val;} else {$ data [$ tmp] ['group _'. $ val ['group'] [] = $ val ;}} print_r ($ data );

I may not make it clear. the result I want is as follows. the result given by two places upstairs is not like this.

Array ([0] => Array ([group] => 0 [name] => guest test1 [sort] => 0) [group_1] => Array ([0] => Array ([group] => 1 [name] => guest test2 [sort] => 1) [1] => Array ([group] => 1 [name] => guest test3 [sort] => 2 )) [3] => Array ([group] => 0 [name] => guest test4 [sort] => 3) [group_2] => Array ([0] => Array ([group] => 2 [name] => guest test5 [sort] => 4) [1] => Array ([group] => 2 [name] => guest test6 [sort] => 5 )))

$ Arr = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 1 => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), 2 => array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 4 => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), 5 => array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5),); $ data = array (); foreach ($ arr as $ k => $ v) {if ($ v ['group'] = 0) {$ data [$ k] = $ v ;} else {$ data ['group _'. $ v ['group'] [] = $ v ;}} print_r ($ data );

$ Arr = array (0 => array ('group' => 0, 'name' => 'guest test1', 'sort '=> 0 ), 1 => array ('group' => 1, 'name' => 'guest test2', 'sort '=> 1 ), 2 => array ('group' => 1, 'name' => 'guest test3', 'sort '=> 2 ), 3 => array ('group' => 0, 'name' => 'guest test4', 'sort '=> 3 ), 4 => array ('group' => 2, 'name' => 'guest test5', 'sort '=> 4 ), 5 => array ('group' => 2, 'name' => 'guest test6', 'sort '=> 5),); $ data = array (); foreach ($ arr as $ k => $ v) {if ($ v ['group'] = 0) {$ data [$ k] = $ v ;} else {$ data ['group _'. $ v ['group'] [] = $ v ;}} print_r ($ data );



Thank you very much.
Related Article

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.