2012-08-04 134 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; 
      } 
     } 
} 

這可能有缺陷,需要更多的工作,但我認爲它至少有一個開始。