Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays - Fatal编程技术网

如果数组可以有任何大小和嵌套,如何从PHP数组中的特定键获取所有数据?

如果数组可以有任何大小和嵌套,如何从PHP数组中的特定键获取所有数据?,php,arrays,Php,Arrays,这是一个非常具体的问题,如果我的描述不够清楚,很抱歉。 我有一个数组,看起来像这样: [conditions] => Array ( [set] => all [set_value] => 1 [conditions] => Array ( [1] => Array (

这是一个非常具体的问题,如果我的描述不够清楚,很抱歉。 我有一个数组,看起来像这样:

    [conditions] => Array
    (
        [set] => all
        [set_value] => 1
        [conditions] => Array
            (
                [1] => Array
                    (
                        [operator] => eq
                        [condition] => price
                        [value] => 500
                    )

                [2] => Array
                    (
                        [set] => all
                        [set_value] => 1
                        [conditions] => Array
                            (
                                [1] => Array
                                    (
                                        [operator] => in
                                        [condition] => users
                                        [value] => 3
                                    )

                            )

                    )

                [3] => Array
                    (
                        [set] => all
                        [set_value] => 1
                        [conditions] => Array
                            (
                                [1] => Array
                                    (
                                        [set] => all
                                        [set_value] => 1
                                        [conditions] => Array
                                            (
                                                [1] => Array
                                                    (
                                                        [operator] => in
                                                        [condition] => categories
                                                        [value] => 168
                                                    )

                                            )

                                    )

                                [2] => Array
                                    (
                                        [operator] => eq
                                        [condition] => payment
                                        [value] => 13
                                    )

                            )

                    )

            )

    )
基本上,它是一个数组,主键是一组“条件”,但每个条件也可以包含另一组条件,每个条件都可以包含另一组条件,等等

这个过程可以不间断地继续下去,我不知道数组将有多少嵌套

我的目标是得到所有的“最终”条件,那些没有其他组的条件。因此,对于我的测试输入,结果应该是:

[1]=>数组
(
[操作员]=>eq
[条件]=>价格
[值]=>500
),
[2] =>阵列
(
[操作员]=>输入
[条件]=>用户
[值]=>3
),
[3] =>阵列
(
[操作员]=>输入
[条件]=>类别
[值]=>168
),
[4] =>阵列
(
[操作员]=>eq
[条件]=>付款
[值]=>13

)
使用递归函数:

function getConditions($array, &$result) {
    if (is_array($array)) {
        if (array_key_exists('conditions', $array)) {
            getConditions($array['conditions'], $result);
        } else {
            foreach($array as $item) {
                if (array_key_exists('condition', $item)) {
                    $result[] = $item;
                } elseif (array_key_exists('conditions', $item)) {
                    getConditions($item['conditions'], $result);
                }
            }
        }
    }
}

$result = [];
getConditions($myConditions, $result);

print_r($result);

成功了!我也有一个类似的想法,但我先做了foreach,之前没有任何if-s。非常感谢!:)