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.