2012-08-04 135 views
0

HI我需要解析一多维数组PHP解析多维数组

$myArray = array(
    array('id' => 6), 
    array(
     'id' => 3, 
     'children' => array(
      'id' => 5, 
      'children' => array(
       'id' => 7, 
       'children' => array(
        array('id' => 4), 
        array('id' => 1) 
       ), 
       array('id' => 8) 
      ) 
     ) 
    ), 
    array('id' => 2) 
); 

下面是输出的,我需要一个字符串或数组...

6 
3 
3,5 
3,5,7 
3,5,7,4 
3,5,7,1 
3,5,8 
2 
+2

你的问题是什么? – dmmd 2012-08-04 15:51:10

+0

Implode和一个简单的循环应该做的。 – Mahn 2012-08-04 15:52:37

+0

这不是一个网站要求这样给我的代码问题,首先你必须尝试。 – 2012-08-04 15:58:28

回答

1

你需要创建一个递归循环:

$children = array(); 

function getChilren($myArray, $children){ 

    foreach($myArray as $value){ 
      if(is_array($value)){ 
       $cLen = count($children); 
       $children[] = $children[$cLen-1]; 
       getChildren($value, $children[$cLen]); 
      } 
      else { 
       $children[] = $value; 
      } 
     } 
} 

这可能有缺陷,需要更多的工作,但我认为它至少有一个开始。