如果值为空,则取消设置数组键

时间:2018-04-11 11:25:19

标签: php arrays

Array
(
    [2] => Array
        (
            [option_id] => 2
            [price] => 15
            [processor] => Array
                (
                    [3] => Array
                        (
                            [processor_id] => 3
                            [price] => 15
                        )

                    [4] => Array
                        (
                            [processor_id] => 4
                            [price] => 15
                        )

                )

        )

    [3] => Array
        (
            [option_id] => 3
            [price] => 15
            [processor] => Array
                (
                    [3] => Array
                        (
                            [processor_id] => 3
                            [price] => 15
                        )

                    [4] => Array
                        (
                            [processor_id] => 4
                            [price] => 15
                        )

                )

        )

    [4] => Array
        (
            [processor] => Array
                (
                    [3] => Array
                        (
                            [price] => // empty value
                        )

                    [4] => Array
                        (
                            [price] => // empty value
                        )

                )

        )

)

我有这个数组,现在我想取消设置数组,哪个数组没有任何值,就像在最后一个数组中没有给定值,所以我想取消整个数组键。

在这个数组中,我有空值,所以如何取消设置[4]键。所以没有foreach循环是可能的。

1 个答案:

答案 0 :(得分:0)

您可以使用array_filter。由于您没有提供任何代码,我建议您阅读php.net上的文档。

function filter_function($var) {
    // return an expression evaluating to true to keep value
    return ($var["option_id"]);
}

$filtered = array_filter($unfiltered, "filter_function");