2014-01-23 59 views
0

在我的數據庫中存儲一個表格,該表格存儲菜單項目,其中每個項目都有一個ID,一個名稱和一個父親ID,我需要對其進行排列並獲得多級樹狀結構。我需要的是一個包含頂層菜單的數組,然後每個元素都帶有包含子菜單的「子元素」數組,而這個子菜單的子元素數組中包含它們各自的子子菜單。英語不是我的母語,所以忍耐着我:)如何從另一個數組中獲取樹狀數組?

一個更好理解的例子。

我在陣列形式以下菜單:

1- System 
    2- Profile 
    3- Account 
    4- Info 
    5- Security 
6- Logout 

用下面的數組:

$array = array(
    array('id' => 1, 'item'=>'System', 'id_father' => null), 
    array('id' => 2, 'item'=>'Profile', 'id_father' => 1), 
    array('id' => 3, 'item'=>'Account', 'id_father' => 2), 
    array('id' => 4, 'item'=>'Info', 'id_father' => 3), 
    array('id' => 5, 'item'=>'Security', 'id_father' => 3), 
    array('id' => 6, 'item'=>'Logout', 'id_father' => 1) 
); 

我怎樣可以得到以下? :

array(
    array('id' => 1, 'item'=>'System', 'id_father' => null, 
    'childs' => array(
     array('id' => 2, 'item'=>'Profile', 'id_father' => 1), 
     array('id' => 3, 'item'=>'Account', 'id_father' => 2, 
      'childs' => array(
       array('id' => 4, 'item'=>'Info', 'id_father' => 3), 
       array('id' => 5, 'item'=>'Security', 'id_father' => 3) 
      ), 
     ), 
    ), 
), 
    array('id' => 6, 'item'=>'Logout', 'id_father' => 1) 
); 
+1

的'id_father'項目'註銷'應該是不可見的「根」,而不是項目1,我噸hink。首先你應該在開始編寫轉換器之前建立一個清晰的第二個結構?或者,如果我的假設是錯誤的,爲什麼項目'安全'是'id_father'爲3而不是4? –

+0

您的陣列與您的示例菜單不一致。無論如何,請考慮遞歸函數調用 – paquino

回答

1

更改$array到:

$array = array(
array('id' => 1, 'item'=>'System', 'id_father' => null), 
array('id' => 2, 'item'=>'Profile', 'id_father' => 1), 
array('id' => 3, 'item'=>'Account', 'id_father' => 1), // set id_father = 1 
array('id' => 4, 'item'=>'Info', 'id_father' => 3), 
array('id' => 5, 'item'=>'Security', 'id_father' => 3), 
array('id' => 6, 'item'=>'Logout', 'id_father' => null) // edited to set id_father = null 
); 

做它:

function tree($ar, $pid = null) { 
$op = array(); 
foreach($ar as $item) { 
    if($item['id_father'] == $pid) { 
     $op[$item['id']] = array(
      'item' => $item['item'], 
      'id_father' => $item['id_father'], 
      'id' => $item['id'] 
     ); 
     // using recursion 
     $children = tree($ar, $item['id']); 
     if($children) { 
      $op[$item['id']]['childs'] = $children; 
     } 
    } 
} 
return $op; 
} 


$tree = tree($array); 

echo '<pre>'; 
print_r($tree); 
echo '</pre>'; 

// OUTPUT 

Array 
(
[1] => Array 
    (
     [item] => System 
     [id_father] => 
     [id] => 1 
     [childs] => Array 
      (
       [2] => Array 
        (
         [item] => Profile 
         [id_father] => 1 
         [id] => 2 
        ) 

       [3] => Array 
        (
         [item] => Account 
         [id_father] => 1 
         [id] => 3 
         [childs] => Array 
          (
           [4] => Array 
            (
             [item] => Info 
             [id_father] => 3 
             [id] => 4 
            ) 

           [5] => Array 
            (
             [item] => Security 
             [id_father] => 3 
             [id] => 5 
            ) 

          ) 

        ) 

      ) 

    ) 

[6] => Array 
    (
     [item] => Logout 
     [id_father] => 
     [id] => 6 
    ) 

) 
0

有沒有必要遞歸以這樣的方式

$pool = array(); 
foreach ($array as $value) { 
    $pool[$value['id']] = $value; 
    $pool[$value['id']]['children'] = array(); 
} 
foreach ($pool as $k => $v) { 
    if ($v['id_father']) { 
     $pool[$v['id_father']]['children'][] = &$pool[$k]; 
    } 
    else 
     $parent[] = $v['id']; 
} 
$result = array(); 
foreach ($parent as $val) { 
    $result = $result + $pool[$val]; 
} 
print_r($result); 
相關問題