从多维数组中提取具有父值的子项

时间:2013-11-06 13:56:52

标签: php multidimensional-array

我有以下多维数组$ array:

Array
(
    [criteria] => default
    [name] => default
    [data] => Array
        (
            [0] => Array
                (
                    [criteria] => test1
                    [name] => test1_name
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [criteria] => test2
                                    [name] => test2_name
                                    [data] => Array
                                        (
                                        )

                                )
                             [1] => Array
                                (
                                    [criteria] => test3
                                    [name] => test3_name
                                    [data] => Array
                                        (
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [criteria] => test4
                    [name] => test4_name
                    [data] => Array
                        (
                        )
                  )
           )
)     

我需要使用之前所有父节点的条件和标准来提取每个节点的名称($ array ['data'])。类似的东西:

Array
(
    Array
     (
        [name] => default
        [criteria] => 
            array(
                [0] => default
            )
        )
     Array
     (
        [name] => test1
        [criteria] => array
        (
            [0] => default
            [1] => test1
        )
     )
     Array
     (
        [name] => test2
        [criteria] => array
        (
            [0] => default
            [1] => test1
            [2] => test2
        )
     )
     Array
     (
        [name] => test3
        [criteria] => array
        (
            [0] => default
            [1] => test1
            [2] => test3
        )
     )
     Array
     (
        [name] => test4
        [criteria] => array
        (
            [0] => default
            [1] => test4
        )
     )
)

请注意,在这种情况下,每个数组的名称字段永远不会在任何地方重复名称。

1 个答案:

答案 0 :(得分:1)

$a = array
(
    'criteria' => 'default',
    'name' => 'default',
    'data' => array
        (
            '0' => Array
                (
                    'criteria' => 'test1',
                    'name' => 'test1_name',
                    'data' => array
                        (
                            '0' => array
                                (
                                    'criteria' => 'test2',
                                    'name' => 'test2_name',
                                    'data' => array
                                        (
                                        )

                                ),
                             '1' => array
                                (
                                    'criteria' => 'test3',
                                    'name' => 'test3_name',
                                    'data' => array
                                        (
                                        )

                                )

                        )

                ),

            '1' => array
                (
                    'criteria' => 'test4',
                    'name' => 'test4_name',
                    'data' => array
                        (
                        )
                  )
           )
);

function buildHelper($nodeList, $currentPath, &$path) {
    foreach($nodeList as $node) {
        // add creteria to current path
        array_push($currentPath, $node['criteria']);
        array_push($path, array('name' => $node['name'], 'citeria' => $currentPath));
        // go throught child list
        if (is_array($node['data']))
            buildHelper($node['data'], $currentPath, $path);
        // remove from current path
        array_pop($currentPath);
    }
}

function build($node) {
    // always from root node
    $currentPath = array($node['criteria']);
    $result = array();
    array_push($result, array('name' => $node['name'], 'citeria' => $currentPath));
    if (is_array($node['data']))
        buildHelper($node['data'], $currentPath, $result);
    return $result;
}

print_r(build($a));
相关问题