Heim > Artikel > Backend-Entwicklung > 数组遍历有关问题
数组遍历问题
$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 />