2016-03-31 66 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

相关问题