过滤掉多维数组的空数组元素 [英] Filter out empty array elements of multidimensional array

查看:66
本文介绍了过滤掉多维数组的空数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤通过将XML转换为数组而创建的数组.我想删除键= 0和一个空值(例如"InvalidKey")的所有父键,但是不删除那些具有自定义名称但没有值(例如"column")的父键.我已经使用过array_filter(甚至与array_map结合使用),但是那些函数将过滤掉数组中的信息太少或太多.我还尝试创建一个loopable函数来检查当前数组是否具有0的键和空值,但是我不知道如何获取当前数组的父键,例如:

I'd like to filter an array which is created by converting XML to an array. I'd like to remove all parent keys of arrays with key = 0 and an empty value (f.e. "InvalidKey"), but not the ones with a custom name and no value (f.e. "column"). I've already used array_filter (even in combination with array_map), but those functions will filter too few or too much information from the array. I've also tried to create a loopable function to check if the current array has a key of 0 and an empty value, but I don't know how to get the parent key of the current array, f.e.:

Array
(
    [InvalidKey] => Array
        (
            [0] => *NULL*
        )
);

key($arrInput) = 0;
parent::key($arrInput) = "InvalidKey";

那么,如何获得:

Array
(
    [test] => 
    [demo] => 524018
    [column] => 
    [xml] => Array
    (
        [Header] => Array
            (
                [0] => Array
                    (
                        [ValidKey] => Array
                            (
                                [0] => 1234
                            )

                        [InvalidKey] => Array
                            (
                                [0] =>
                            )
                    )

            )

        [Body] => Array
            (
                [0] => *NULL*
            )

        [Footer] => Array
            (
                [0] => Array
                    (
                        [ValidKey] => Array
                            (
                                [0] => I am valid
                            )

                        [MoreValidKey] => Array
                            (
                                [0] => I am valid too
                            )

                        [InvalidKey] => Array
                            (
                                [0] =>
                            )
                    )

            )

    )
)

收件人:

Array
(
    [test] => 
    [demo] => 524018
    [column] => 
    [xml] => Array
    (
        [Header] => Array
            (
                [0] => Array
                    (
                        [ValidKey] => Array
                            (
                                [0] => 1234
                            )
                    )

            )

        [Footer] => Array
            (
                [0] => Array
                    (
                        [ValidKey] => Array
                            (
                                [0] => I am valid
                            )

                        [MoreValidKey] => Array
                            (
                                [0] => I am valid too
                            )
                    )

            )

    )
)

PS:使用的阵列键名称是可变的.为简单起见,我使用了(In)ValidKey".该数组可以具有尽可能多的深度,所以我不能满足于2个for循环.

PS: The used array key names are variable. For simplicity I used "(In)ValidKey". The array can be as much levels deep as possible, so I can't suffice with 2 for loops.

推荐答案

您需要编写一个自定义脚本来检查数组的所有元素.

You need to write a custom script which checks all elements of your array.

示例:

<?php
$test = [
    [
        [
            "asdf",
            "",
            0,
            false,
            true,
            [
                "asdf",
                "",
                []
            ]
        ],
        []
    ],
    []
];

function removeEmptyElements(array $array)
{
    foreach ($array as $key => $value) {
        if (is_array($value))
            $value = removeEmptyElements($value);

        if (empty($value) && false !== $value && 0 !== $value)
            unset($array[$key]);
        else
            $array[$key] = $value;
    }

    return $array;
}

print_r(removeEmptyElements($test));

这篇关于过滤掉多维数组的空数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆