Maison > développement back-end > tutoriel php > php数组循环问题

php数组循环问题

WBOY
Libérer: 2016-06-20 12:56:13
original
830 Les gens l'ont consulté

$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),    	);
Copier après la connexion


原数组如上,希望循环得到以下结果:
$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),    			)    	);
Copier après la connexion


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


回复讨论(解决方案)

$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);
Copier après la connexion
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                        )                )        ))
Copier après la connexion

$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);
Copier après la connexion

可能没说清楚,我要的结果是如下的,楼上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                )        ))
Copier après la connexion

$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);
Copier après la connexion
Copier après la connexion

$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);
Copier après la connexion
Copier après la connexion



非常感谢 
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal