Home > Backend Development > PHP Tutorial > 数组遍历问题

数组遍历问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:44:03
Original
926 people have browsed it

$arr=array(	'10'=>array(		'12'=>0	),	'15'=>array(		'30'=>array(			'35'=>0		),		'31'=>array(			'40'=>0		)	))
Copy after login


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

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


回复讨论(解决方案)

$arr=array('10'=>array('12'=>0),'15'=>array('30'=>array('35'=>0),'31'=>array('40'=>0)));				$a=array();		foreach ($arr as $k=>$v){			$a[$k][]=$k;			if(is_array($v)){				foreach ($v as $kk=>$vv){					$a[$k][]=$kk;				}			}				}		var_dump($a);
Copy after login

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

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

$arr=array(  '10'=>array(    '12'=>0  ),  '15'=>array(    '30'=>array(      '35'=>0    ),    '31'=>array(      '40'=>0    )  ));print_r(foo($arr));function foo($ar) {  $res = array();  foreach($ar as $k=>$v) {    if(is_array($v)) {      foreach(foo($v) as $r) $res[] = "$k,$r";    }else $res[] = $k;  }  return $res;}
Copy after login
Array(    [0] => 10,12    [1] => 15,30,35    [2] => 15,31,40)
Copy after login

版主也太牛叉了!昨天我只想到用递归但是没有弄出来…

写了个方法

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


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


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

$arr=array(  '10'=>array(    '12'=>0  ),  '15'=>array(    '30'=>array(      '35'=>0    ),    '31'=>array(      '40'=>0    )  ));function my($arr,$pid=10){    static $item=array();    static $s='';    foreach($arr as $k=>$v){         if(is_array($v)){            foreach($v as $key=>$value){                $s.=$key.',';                if(is_array($value)){                    my($value,$k);                }            }         }else{            $s.=$k;         }         $s=$pid.','.$s;         $item[]=$s;         $s='';    }    return $item;}$arr=my($arr);array_pop($arr);print_r($arr);
Copy after login


Array(    [0] => 10,12,    [1] => 15,30,35    [2] => 15,31,40)
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