php数组循环问题
程序员文章站
2022-05-08 20:03:43
...
$arr = array( 0 => array('group' => 0, 'name' => '嘉宾test1', 'sort' => 0), 1 => array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), 2 => array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 4 => array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), 5 => array('group' => 2, 'name' => '嘉宾test6', 'sort' => 5), );
原数组如上,希望循环得到以下结果:
$rs = array( 0 => array('group' => 0, 'name' => '嘉宾test1', 'sort' => 0), 'group_1' => array( array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), ), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 'group_2' => array( array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), array('group' => 2, 'name' => '嘉宾test6', 'sort' => 5), ) );
能把分组提取出来,同时保留排序,自己总是写不出来,请大神帮忙
回复讨论(解决方案)
$arr = array( 0 => array('group' => 0, 'name' => '嘉宾test1', 'sort' => 0), 1 => array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), 2 => array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 4 => array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), 5 => array('group' => 2, 'name' => '嘉宾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] => 嘉宾test1 [sort] => 0 [group_1] => Array ( [0] => Array ( [group] => 1 [name] => 嘉宾test2 [sort] => 1 ) [1] => Array ( [group] => 1 [name] => 嘉宾test3 [sort] => 2 ) ) ) [3] => Array ( [group] => 0 [name] => 嘉宾test4 [sort] => 3 [group_1] => Array ( [0] => Array ( [group] => 2 [name] => 嘉宾test5 [sort] => 4 ) [1] => Array ( [group] => 2 [name] => 嘉宾test6 [sort] => 5 ) ) ))
$arr = array( 0 => array('group' => 0, 'name' => '嘉宾test1', 'sort' => 0), 1 => array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), 2 => array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 4 => array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), 5 => array('group' => 2, 'name' => '嘉宾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);
可能没说清楚,我要的结果是如下的,楼上2位给的结果不是这样的
Array( [0] => Array ( [group] => 0 [name] => 嘉宾test1 [sort] => 0 ) [group_1] => Array ( [0] => Array ( [group] => 1 [name] => 嘉宾test2 [sort] => 1 ) [1] => Array ( [group] => 1 [name] => 嘉宾test3 [sort] => 2 ) ) [3] => Array ( [group] => 0 [name] => 嘉宾test4 [sort] => 3 ) [group_2] => Array ( [0] => Array ( [group] => 2 [name] => 嘉宾test5 [sort] => 4 ) [1] => Array ( [group] => 2 [name] => 嘉宾test6 [sort] => 5 ) ))
$arr = array( 0 => array('group' => 0, 'name' => '嘉宾test1', 'sort' => 0), 1 => array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), 2 => array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 4 => array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), 5 => array('group' => 2, 'name' => '嘉宾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' => '嘉宾test1', 'sort' => 0), 1 => array('group' => 1, 'name' => '嘉宾test2', 'sort' => 1), 2 => array('group' => 1, 'name' => '嘉宾test3', 'sort' => 2), 3 => array('group' => 0, 'name' => '嘉宾test4', 'sort' => 3), 4 => array('group' => 2, 'name' => '嘉宾test5', 'sort' => 4), 5 => array('group' => 2, 'name' => '嘉宾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);
非常感谢