首页  >  文章  >  后端开发  >  数组遍历有关问题

数组遍历有关问题

WBOY
WBOY原创
2016-06-13 12:10:57879浏览

数组遍历问题

$arr=array(<br />	'10'=>array(<br />		'12'=>0<br />	),<br />	'15'=>array(<br />		'30'=>array(<br />			'35'=>0<br />		),<br />		'31'=>array(<br />			'40'=>0<br />		)<br />	)<br />)


有以上这个数组,怎样才能得到从顶端到每一个根节点的路径?

比如最后得到这样的结果:
$a[]='10,12';<br />$a[]='15,30,35';<br />$a[]='15,31,40';

------解决思路----------------------
$arr=array('10'=>array('12'=>0),'15'=>array('30'=>array('35'=>0),'31'=>array('40'=>0)));		<br />		$a=array();<br />		foreach ($arr as $k=>$v){<br />			$a[$k][]=$k;<br />			if(is_array($v)){<br />				foreach ($v as $kk=>$vv){<br />					$a[$k][]=$kk;<br />				}<br />			}		<br />		}<br />		var_dump($a);

------解决思路----------------------
<br />$arr=array('10'=>array('12'=>0),'15'=>array('30'=>array('35'=>0),'31'=>array('40'=>0),'33'=>array('42'=>0)));		<br />		$a=array();<br />		$i=0;<br />		foreach ($arr as $k=>$v){			<br />			$a[$i][]=$k;<br />			if(is_array($v)){<br />				foreach ($v as $kk=>$vv){<br />					$a[$i][]=$kk;<br />					if(is_array($vv)){<br />						foreach ($vv as $kkk=>$vvv){<br />							$j=0;<br />							if($j==0){<br />								unset($a[$i]);<br />								$a[$i.'-'.$i][]=$k;<br />								$a[$i.'-'.$i][]=$kk;<br />								$a[$i.'-'.$i][]=$kkk;<br />							}else{<br />								$a[$i][]=$kkk;<br />							}<br />						}<br />					}<br />					$i++;<br />				}<br />			}<br />			$i++;<br />		}<br />		var_dump($a);<br />

------解决思路----------------------
<br />最后是这样<br />$arr=array('10'=>array('12'=>0),'15'=>array('30'=>array('35'=>0),'31'=>array('40'=>0),'33'=>array('42'=>0)));		<br />		$a=array();<br />		$i=0;<br />		foreach ($arr as $k=>$v){			<br />			$a[$i][]=$k;<br />			if(is_array($v)){<br />				foreach ($v as $kk=>$vv){<br />					$a[$i][]=$kk;<br />					if(is_array($vv)){<br />						foreach ($vv as $kkk=>$vvv){							<br />							unset($a[$i]);<br />							$a[$i.'-'.$i][]=$k;<br />							$a[$i.'-'.$i][]=$kk;<br />							$a[$i.'-'.$i][]=$kkk;<br />						}<br />					}<br />					$i++;<br />				}<br />			}<br />		}<br />		var_dump(array_values($a));

------解决思路----------------------
$arr=array(<br />  '10'=>array(<br />    '12'=>0<br />  ),<br />  '15'=>array(<br />    '30'=>array(<br />      '35'=>0<br />    ),<br />    '31'=>array(<br />      '40'=>0<br />    )<br />  )<br />);<br /><br />print_r(foo($arr));<br /><br />function foo($ar) {<br />  $res = array();<br />  foreach($ar as $k=>$v) {<br />    if(is_array($v)) {<br />      foreach(foo($v) as $r) $res[] = "$k,$r";<br />    }else $res[] = $k;<br />  }<br />  return $res;<br />}<br />
Array<br />(<br />    [0] => 10,12<br />    [1] => 15,30,35<br />    [2] => 15,31,40<br />)<br /><br />

------解决思路----------------------
写了个方法
<br />$arr=array(<br />    '10'=>array(<br />        '12'=>0,<br />        '13'=>array(<br />            '14' => 0,<br />            '16' => array(<br />                '17' =>0<br />            )<br />        )<br />    ),<br />    '15'=>array(<br />        '30'=>array(<br />            '35'=>0<br />        ),<br />        '31'=>array(<br />            '40'=>0<br />        )<br />    )<br />);<br /><br />function foo( $arr, &$str = '' )<br />{<br />    foreach($arr as $key => $value)<br />    {<br />        $str .= "," . $key;<br />        if(is_array($value))<br />        {<br />            foo($value, $str);<br /><br />        }else{<br />            print_r( ltrim($str,',') );<br />            echo '<br>';<br />        }<br />        $str = rtrim( $str, ','.$key);<br />    }<br /><br />}<br /><br />foo($arr);<br />

------解决思路----------------------

<br />$arr=array(<br />    '10'=>array(<br />        '12'=>0,<br />        '13'=>array(<br />            '14' => 0,<br />            '16' => array(<br />                '17' =>0<br />            )<br />        )<br />    ),<br />    '15'=>array(<br />        '30'=>array(<br />            '35'=>0<br />        ),<br />        '31'=>array(<br />            '40'=>0<br />        )<br />    )<br />);<br /><br />function foo( $arr, &$str = '' )<br />{<br />    foreach($arr as $key => $value)<br />    {<br />        $str .= "," . $key;<br />        if(is_array($value))<br />        {<br />            foo($value, $str);<br /><br />        }else{<br />            print_r( ltrim($str,',') );<br />            echo '<br>';<br />        }<br />        $str = rtrim( $str, ','.$key);<br />    }<br /><br />}<br /><br />foo($arr);<br />

------解决思路----------------------

<br />$arr=array(<br />    '10'=>array(<br />        '12'=>0,<br />        '13'=>array(<br />            '14' => 0,<br />            '16' => array(<br />                '17' =>0<br />            )<br />        )<br />    ),<br />    '15'=>array(<br />        '30'=>array(<br />            '35'=>0<br />        ),<br />        '31'=>array(<br />            '40'=>0<br />        )<br />    )<br />);<br /><br />function foo( $arr, &$str = '' )<br />{<br />    foreach($arr as $key => $value)<br />    {<br />        $str .= "," . $key;<br />        if(is_array($value))<br />        {<br />            foo($value, $str);<br /><br />        }else{<br />            print_r( ltrim($str,',') );<br />            echo '<br>';<br />        }<br />        $str = rtrim( $str, ','.$key);<br />    }<br /><br />}<br /><br />foo($arr);<br />

------解决思路----------------------
<br />$arr=array(<br />  '10'=>array(<br />    '12'=>0<br />  ),<br />  '15'=>array(<br />    '30'=>array(<br />      '35'=>0<br />    ),<br />    '31'=>array(<br />      '40'=>0<br />    )<br />  )<br />);<br />function my($arr,$pid=10){<br />    static $item=array();<br />    static $s='';<br />    foreach($arr as $k=>$v){<br />         if(is_array($v)){<br />            foreach($v as $key=>$value){<br />                $s.=$key.',';<br />                if(is_array($value)){<br />                    my($value,$k);<br />                }<br />            }<br />         }else{<br />            $s.=$k;<br />         }<br />         $s=$pid.','.$s;<br />         $item[]=$s;<br />         $s='';<br />    }<br />    return $item;<br />}<br />$arr=my($arr);<br />array_pop($arr);<br />print_r($arr);<br />


<br />Array<br />(<br />    [0] => 10,12,<br />    [1] => 15,30,35<br />    [2] => 15,31,40<br />)<br /><br />

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn