PHP从多级数组中获取子集

时间:2012-02-25 11:34:01

标签: php arrays multidimensional-array

我有一个多维数组(简化):

[1] => Array
    (
        [id] => 1
        [id1] => 0
    )
[2] => Array
    (
        [id] => 2
        [id1] => 0
        [children] => Array
            (
                [1001] => Array
                    (
                        [id] => 1001
                        [id1] => 2
                    )
            )
    )
[3] => Array
    (
        [id] => 3
        [id1] => 0
        [children] => Array
            (
                [1002] => Array
                    (
                        [id] => 1002
                        [id1] => 2
                        [children] => Array
                            (
                                [1002] => Array
                                    (
                                        [id] => 1002
                                        [id1] => 2
                                    )
                                [1003] => Array
                                    (
                                        [id] => 1003
                                        [id1] => 2
                                    )
                             )
                    )
                [1003] => Array
                    (
                        [id] => 1003
                        [id1] => 2
                    )
            )
    )

我想选择一部分(作为数组),并附上所有孩子。

示例1(键3,无限深度):

$result = getArrayKey(3, $dept = 0);

[3] => Array
    (
        [id] => 3
        [id1] => 0
        [children] => Array
            (
                [1002] => Array
                    (
                        [id] => 1002
                        [id1] => 2
                        [children] => Array
                            (
                                [1002] => Array
                                    (
                                        [id] => 1002
                                        [id1] => 2
                                    )
                                [1003] => Array
                                    (
                                        [id] => 1003
                                        [id1] => 2
                                    )
                             )
                    )
                [1003] => Array
                    (
                        [id] => 1003
                        [id1] => 2
                    )
            )

示例2(键3,部门1):

$result = getArrayKey(3, $dept = 1);

[3] => Array
    (
        [id] => 3
        [id1] => 0
    )

示例3(键3,无限深度):

$result = getArrayKey(1002, $dept = 0);

[1002] => Array
    (
        [id] => 1002
        [id1] => 2
        [children] => Array
            (
                [1002] => Array
                    (
                        [id] => 1002
                        [id1] => 2
                    )
                [1003] => Array
                    (
                        [id] => 1003
                        [id1] => 2
                    )
            )
    )

我可以遍历这个数组并将所需的级别保存到我认为的新数组中,但有更简单的方法吗?

1 个答案:

答案 0 :(得分:2)

您必须编写一个递归函数,该函数遍历整个数组并拖动深度以确定每个子数组是否应包含在结果中。使用0表示“一切”是非常不切实际的。在我的解决方案中,您要么不给出深度,要么使用负值来检索所有内容。

function getSubArray (array $array, $key, $depth = -1) {
    $result = array();
    foreach ($array[$key] as $k => $v) {
       if (is_array($v) && ($depth != 0)) {
           $result[$k] = getSubArray($array[$key], $k, ($depth - 1));
       } else {
           $result[$k] = $v;
       }
    }
    return $result;
}