如何按维度级别对数组进行分组?

时间:2016-03-31 15:07:29

标签: php arrays

我有一个树形数组,但我想按维度对数组进行分组,以便我可以在自己的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>

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这是一个简单的递归函数示例,它将分隔树数组的每个级别。

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

因为// 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);