2016-03-31 64 views
-1

我有一個樹型數組,但我想按維度對數組進行分組,以便我可以在它自己的DIV中顯示樹的每個級別。如何按維度級別對數組進行分組?

我的樹陣爲:

$tree = 
array(
    1 => array('id'=>1, 'title'=>'Category 1', 'parent_id'=>0, 
     'children'=> array(
      2 => array('id'=>2, 'title'=>'Category 2', 'parent_id'=>1, 'children'=>''), 
      3 => array('id'=>3,'title'=>'Category 3','parent_id'=>1,'children'=>''), 
      4 => array('id'=>4, 'title'=>'Category 4', 'parent_id'=>1, 
       'children'=> array(
        8 => array('id'=>8, 'title'=>'Category 8', 'parent_id'=>4, 'children'=>''), 
        9 => array('id'=>9, 'title'=>'Category 9', 'parent_id'=>4, 'children'=>''), 
        10 => array('id'=>10, 'title'=>'Category 10', 'parent_id'=>4, 
         'children'=> array(
          11 => array('id'=>11, 'title'=>'Category 11', 'parent_id'=>10, 'children'=>''), 
          12 => array('id'=>12, 'title'=>'Category 12', 'parent_id'=>10, 'children'=>''), 
          13 => array('id'=>13, 'title'=>'Category 13', 'parent_id'=>10, 'children'=>'') 
         ) 
        ) 
       ) 
      ) 
     ) 
    ), 
    5 => array('id'=>5, 'title'=>'Category 5', 'parent_id'=>0, 
     'children'=>array(
      6 => array('id'=>6,'title'=>'Category 6', 'parent_id'=>5, 'children'=>''), 
      7 => array('id'=>3,'title'=>'Category 7', 'parent_id'=>5, 'children'=>'') 
     ) 
    ) 
); 

我想每個級別將像this。我對分組輸出的想法是這樣的:

$grouped = array(
    0 => array(
     1 => array('id'=>1, 'title'=>'Category 1', 'parent_id'=>0), 
     5 => array('id'=>5, 'title'=>'Category 5', 'parent_id'=>0) 
    ), 
    1 => array(
     2 => array('id'=>2, 'title'=>'Category 2', 'parent_id'=>1), 
     3 => array('id'=>3, 'title'=>'Category 3', 'parent_id'=>1), 
     4 => array('id'=>4, 'title'=>'Category 4', 'parent_id'=>1), 
     6 => array('id'=>6, 'title'=>'Category 6', 'parent_id'=>5), 
     7 => array('id'=>3, 'title'=>'Category 7', 'parent_id'=>5) 
    ), 
    2 => array(
     8 => array('id'=>8, 'title'=>'Category 8', 'parent_id'=>4), 
     9 => array('id'=>9, 'title'=>'Category 9', 'parent_id'=>4), 
     10 => array('id'=>10, 'title'=>'Category 10', 'parent_id'=>4) 
    ), 
    3 => array(
     11 => array('id'=>11, 'title'=>'Category 11', 'parent_id'=>10), 
     12 => array('id'=>12, 'title'=>'Category 12', 'parent_id'=>10), 
     13 => array('id'=>13, 'title'=>'Category 13', 'parent_id'=>10) 
    ) 
); 

別的東西是好的,如果你有其他的想法,但我希望能夠foreach輸出數組,並在不同的顯示每個級別中的所有元素DIV。

EX:

<div id="box-1"> 
    Group Array[1] 
</div> 
<div id="box-2"> 
    Group Array[2] 
</div> 
<div id="box-3"> 
    Group Array[3] 
</div> 
<div id="box-4"> 
    Group Array[4] 
</div> 

我怎樣才能做到這一點?

+2

請參閱[問]和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。 – Rizier123

+0

您可以遞歸執行此操作。編寫一個函數,將當前級別的所有信息添加到列表中,然後(在該函數內)在「children」屬性上調用函數本身。 – jojonas

+0

我編輯了你的問題,從圖像中添加代碼,並澄清我認爲是你的預期目標。如果不是您想的內容,請隨時重新編輯或回滾這些更改,並且如果您可以添加您編寫的任何代碼以實現此目的,則可能有助於進一步澄清問題。 –

回答

2

下面是一個簡單遞歸函數的例子,它將分隔樹數組的每個級別。

// begin with a reference to an empty array and level 0 
function split_levels($branches, &$output = array(), $level = 0) { 

    // loop over each element of the current branch 
    foreach ($branches as $id => $branch) { 

     // if the element has children, make a recursive call with an incremented level 
     if ($branch['children']) split_levels($branch['children'], $output, $level + 1); 

     // remove the children (this is optional) 
     unset($branch['children']); 

     // add the element to the output array at the appropriate level 
     $output[$level][$id] = $branch; 
    } 
} 

因爲$output是在這個函數它應該被稱爲像這樣的引用:

split_levels($tree, $output); 

,其結果將是$output

+0

它的工作!謝謝你。 :) – user3514448

相關問題