Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多维数组中提取具有父值的子级_Php_Multidimensional Array - Fatal编程技术网

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

Php 从多维数组中提取具有父值的子级,php,multidimensional-array,Php,Multidimensional Array,我有以下多维数组$array: Array ( [criteria] => default [name] => default [data] => Array ( [0] => Array ( [criteria] => test1 [name] => test1_name

我有以下多维数组$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
        )
     )
)

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

您是一位提出这一雄辩解决方案的非常善良的人。您是一位提出这一雄辩解决方案的非常善良的人。
$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));