首页 > 后端开发 > php教程 > 数组重组

数组重组

WBOY
发布: 2016-06-23 14:18:21
原创
935 人浏览过

PHP 数组

#原数组$list = array(	0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),	1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),	2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),	3=>array('mid'=>2,'menu'=>'m2','level'=>1,'belong'=>0,'higher'=>' '),	4=>array('mid'=>3,'menu'=>'m3','level'=>1,'belong'=>0,'higher'=>' '),	5=>array('mid'=>4,'menu'=>'m4','level'=>1,'belong'=>0,'higher'=>'d1'),	6=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e1'),	7=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e2')	);#从原数组中提取新数组$menuArr = array(	0=>array('mid'=>1,'main'=>'m1','child'=>array('a1','a2','a3') ),	1=>array('mid'=>2,'main'=>'m2','child'=>' ',	2=>array('mid'=>3,'main'=>'m3','child'=>' ',	3=>array('mid'=>4,'main'=>'m4','child'=>array('d1') ),	4=>array('mid'=>5,'main'=>'m5','child'=>array('e1','e2') ),);#自己搞半天搞晕了:$sign = null;$n = 0;$menuArr = array();foreach($list as $men){	if($sign == $men['mid']){		echo "n:{$n}<br>";		$menuArr[$men['mid']]['main'] = $men['menu'];		$menuArr[$men['mid']]['child'][$n] = $men['higher'];  #提示这里不可解析		//array_push($menuArr[$men['mid']]['child'], $men['higher']);		$n++;	}else{		$n = 0;		$menuArr[$men['mid']]['main'] = $men['menu'];		$menuArr[$men['mid']]['child'] = $men['higher'];	}	$sign = $men['mid'];}print_r($menuArr);#帮帮忙……
登录后复制


回复讨论(解决方案)

$list = array(    0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),    1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),    2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),    3=>array('mid'=>2,'menu'=>'m2','level'=>1,'belong'=>0,'higher'=>' '),    4=>array('mid'=>3,'menu'=>'m3','level'=>1,'belong'=>0,'higher'=>' '),    5=>array('mid'=>4,'menu'=>'m4','level'=>1,'belong'=>0,'higher'=>'d1'),    6=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e1'),    7=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e2')    );$menuArr = array();foreach($list as $v) {  if(! isset($menuArr[$v['mid']]))    $menuArr[$v['mid']] = array('mid' => $v['mid'], 'main' => $v['menu'], 'child' => array($v['higher']));  else $menuArr[$v['mid']]['child'][] = $v['higher'];}$menuArr = array_values($menuArr);print_r($menuArr);
登录后复制
Array
(
[0] => Array
(
[mid] => 1
[main] => m1
[child] => Array
(
[0] => a1
[1] => a2
[2] => a3
)

)

[1] => Array
(
[mid] => 2
[main] => m2
[child] => Array
(
[0] =>
)

)

[2] => Array
(
[mid] => 3
[main] => m3
[child] => Array
(
[0] =>
)

)

[3] => Array
(
[mid] => 4
[main] => m4
[child] => Array
(
[0] => d1
)

)

[4] => Array
(
[mid] => 5
[main] => m5
[child] => Array
(
[0] => e1
[1] => e2
)

)

)

为什么你的第6行

else $menuArr[$v['mid']]['child'][] = $v['higher'];}
登录后复制
登录后复制
没有报错?
我的这样写
 $menuArr[$men['mid']]['child'][] = $men['higher']; 
登录后复制
登录后复制

提示:Fatal error: [] operator not supported for strings?

为什么你的第6行

else $menuArr[$v['mid']]['child'][] = $v['higher'];}
登录后复制
登录后复制
没有报错?
我的这样写
 $menuArr[$men['mid']]['child'][] = $men['higher']; 
登录后复制
登录后复制

提示:Fatal error: [] operator not supported for strings?

不明白你$sign == $men['mid']的作用,$men['mid']都不可能是空。

用你的代码改了一下,可以了。

#原数组$list = array(    0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),    1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),    2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),    3=>array('mid'=>2,'menu'=>'m2','level'=>1,'belong'=>0,'higher'=>' '),    4=>array('mid'=>3,'menu'=>'m3','level'=>1,'belong'=>0,'higher'=>' '),    5=>array('mid'=>4,'menu'=>'m4','level'=>1,'belong'=>0,'higher'=>'d1'),    6=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e1'),    7=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e2')    );$menuArr = array();foreach($list as $men){    if(!isset($menuArr[$men['mid']])){        $menuArr[$men['mid']]['mid'] = $men['mid'];        $menuArr[$men['mid']]['main'] = $men['menu'];        $menuArr[$men['mid']]['child'] = array($men['higher']);  #提示这里不可解析    }else{        array_push($menuArr[$men['mid']]['child'],$men['higher']);    }}$menuArr = array_values($menuArr);echo '<pre class="brush:php;toolbar:false">';print_r($menuArr);echo '
登录后复制
';

不明白你$sign == $men['mid']的作用,$men['mid']都不可能是空。
没有说$men['mid']值是空,在我的代码的寓意是指当数组中$men['mid']的值相同时执行...

<?php#原数组$list = array(	0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),	1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),	2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),	3=>array('mid'=>2,'menu'=>'m2','level'=>1,'belong'=>0,'higher'=>' '),	4=>array('mid'=>3,'menu'=>'m3','level'=>1,'belong'=>0,'higher'=>' '),	5=>array('mid'=>4,'menu'=>'m4','level'=>1,'belong'=>0,'higher'=>'d1'),	6=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e1'),	7=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e2')	);#从原数组中提取新数组$menuArr = array(	0=>array('mid'=>1,'main'=>'m1','child'=>array('a1','a2','a3') ),	1=>array('mid'=>2,'main'=>'m2','child'=>' '),	2=>array('mid'=>3,'main'=>'m3','child'=>' '),	3=>array('mid'=>4,'main'=>'m4','child'=>array('d1') ),	4=>array('mid'=>5,'main'=>'m5','child'=>array('e1','e2') ));#自己搞半天搞晕了:$sign = null;$n = 0; $menuArr = array();foreach($list as $men){	if($sign == $men['mid']){		echo "n:{$n}<br>";		$menuArr[$men['mid']]['main'] = $men['menu'];		// index = 1时, $menuArr[$men['mid']]['child'] = 'a1', 一个字符串当作数组用你说能不出错么?		$menuArr[$men['mid']]['child'][$n] = $men['higher'];  #提示这里不可解析		//array_push($menuArr[$men['mid']]['child'], $men['higher']);		$n++;	}else{		$n = 1;		$menuArr[$men['mid']]['main'] = $men['menu'];		// index = 0时, $menuArr[$men['mid']]['child'] = 'a1'		//$menuArr[$men['mid']]['child'] = $men['higher'];		$menuArr[$men['mid']]['child'] = $men['higher'] != ' ' ? array($men['higher']) : '';	}		$sign = $men['mid'];}print_r($menuArr);#帮帮忙……
登录后复制

$menuArr[$men['mid']]['child'] = $men['higher'];
后,$menuArr[$men['mid']]['child'] 是字符串
你再当做数组操作
$menuArr[$men['mid']]['child'][$n] = $men['higher'];
当然要报错

原来这样啊,看来最近真的晕头了,O(∩_∩)O谢谢帮忙。

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