php数组循环问题

WBOY
Lepaskan: 2016-06-20 12:56:13
asal
828 orang telah melayarinya

$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),    	);
Salin selepas log masuk


原数组如上,希望循环得到以下结果:
$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),    			)    	);
Salin selepas log masuk


能把分组提取出来,同时保留排序,自己总是写不出来,请大神帮忙


回复讨论(解决方案)

$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);
Salin selepas log masuk
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                        )                )        ))
Salin selepas log masuk

$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);
Salin selepas log masuk

可能没说清楚,我要的结果是如下的,楼上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                )        ))
Salin selepas log masuk

$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);
Salin selepas log masuk
Salin selepas log masuk

$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);
Salin selepas log masuk
Salin selepas log masuk



非常感谢 
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan