2014-03-31 66 views
0

如何將此結構顯示爲多級菜單?php顯示多級treenode菜單

這裏的結構:

array 
    0 => 
    array 
     'product_category_code' => string 'bracelets' (length=9) 
     'product_category_desc' => string 'Bracelets' (length=9) 
     'parent_node' => string '' (length=0) 
     'inactive' => string '0' (length=1) 
     'sort' => string '0' (length=1) 
     'created_by' => string '1' (length=1) 
     'created_date' => string '2014-03-14 22:04:08' (length=19) 
     'modified_by' => string '1' (length=1) 
     'modified_date' => string '2014-03-14 22:09:05' (length=19) 
    1 => 
    array 
     'product_category_code' => string 'floral-dress' (length=12) 
     'product_category_desc' => string 'Floral Dress' (length=12) 
     'parent_node' => string '' (length=0) 
     'inactive' => string '0' (length=1) 
     'sort' => string '0' (length=1) 
     'created_by' => string '1' (length=1) 
     'created_date' => string '2014-03-14 22:09:49' (length=19) 
     'modified_by' => string '1' (length=1) 
     'modified_date' => string '2014-03-30 19:06:58' (length=19) 
    2 => 
    array 
     'product_category_code' => string 'flowery-bracelets' (length=17) 
     'product_category_desc' => string 'Flowery Bracelets' (length=17) 
     'parent_node' => string 'bracelets' (length=9) 
     'inactive' => string '0' (length=1) 
     'sort' => string '0' (length=1) 
     'created_by' => string '1' (length=1) 
     'created_date' => string '2014-03-14 22:09:16' (length=19) 
     'modified_by' => string '1' (length=1) 
     'modified_date' => string '2014-03-30 19:08:44' (length=19) 
    3 => 
    array 
     'product_category_code' => string 'small-flowery-bracelets' (length=23) 
     'product_category_desc' => string 'Small Flowery Bracelets' (length=23) 
     'parent_node' => string 'flowery-bracelets' (length=17) 
     'inactive' => string '0' (length=1) 
     'sort' => string '0' (length=1) 
     'created_by' => string '1' (length=1) 
     'created_date' => string '2014-03-14 22:08:35' (length=19) 
     'modified_by' => string '1' (length=1) 
     'modified_date' => string '2014-03-30 19:09:44' (length=19) 
    4 => 
    array 
     'product_category_code' => string 'summer-dress' (length=12) 
     'product_category_desc' => string 'Summer Dress' (length=12) 
     'parent_node' => string '' (length=0) 
     'inactive' => string '0' (length=1) 
     'sort' => string '0' (length=1) 
     'created_by' => string '1' (length=1) 
     'created_date' => string '2014-03-14 22:09:29' (length=19) 
     'modified_by' => string '0' (length=1) 
     'modified_date' => null 

和輸出應該是這樣的:

  • 手鍊
    • 華麗的手鍊
      • 小華麗的手鍊
  • 碎花連衣裙
  • 夏裝

這裏是我做的,但它仍然顯示的子節點

function getChildren($rows, $p = 0) { 
$r = array(); 
foreach($rows as $row) { 
    if ($row['parent_node']==$p) { 
     var_dump($p); 
     $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']); 
    } 
} 
return $r; 

謝謝!

回答

2

這是因爲當您已經分配數組時,您仍然有數組中的類別。你可以做的是在你傳遞參數作爲參考的地方完成這個功能,並且在foreach循環中有能力從已經分配的類別中清除數組。簡單的實現如下。

function getChildren(&$rows, $p = 0) { 
    $r = array(); 
    foreach($rows as $row_id => $row) { 
     if ($row['parent_node']==$p) { 
      $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']); 
      unset($rows[$row_id]); 
     } 
    } 
    return $r; 
} 
+0

Thanks ailvenge!正是我需要的!我注意到參數上$行的「&」。你能解釋一下它是什麼嗎? – basagabi

+0

您將參數作爲參數傳遞給內存中的變量 - 您可以隨後更新數組/刪除行。當你刪除&時,php會傳遞數組副本而不是原始數組,所以即使你刪除了一個元素,你仍然從數組副本中刪除它,所以它不會對原始數據產生任何影響array :) – sunshinejr

+0

Ailvenge,我注意到有一個問題,當有一個父節點的記錄之前是父母,不會顯示爲小孩。給定上面的數組,如果Bracelets具有Floral Dress的父節點,則Bracelets不會顯示爲子節點。 – basagabi