数组遍历有关问题

WBOY
Release: 2016-06-13 12:10:57
Original
902 people have browsed it

数组遍历问题

$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 />)
Copy after login


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

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

------解决思路----------------------
$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);
Copy after login

------解决思路----------------------
<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 />
Copy after login

------解决思路----------------------
<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));
Copy after login

------解决思路----------------------
$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 />
Copy after login
Array<br />(<br />    [0] => 10,12<br />    [1] => 15,30,35<br />    [2] => 15,31,40<br />)<br /><br />
Copy after login

------解决思路----------------------
写了个方法
<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 />
Copy after login
Copy after login
Copy after login

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

<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 />
Copy after login
Copy after login
Copy after login

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

<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 />
Copy after login
Copy after login
Copy after login

------解决思路----------------------
<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 />
Copy after login


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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template