首页 > 后端开发 > php教程 > php数组循环问题

php数组循环问题

WBOY
发布: 2016-06-20 12:56:13
原创
830 人浏览过

$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);
登录后复制
登录后复制



非常感谢 
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板