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";

So, how to get from:

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] =>
                            )
                    )

            )

    )
)

To:

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: 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.

  • 1
    Possible duplicate of [How to remove empty values from multidimensional array in PHP?](https://stackoverflow.com/questions/10214531/how-to-remove-empty-values-from-multidimensional-array-in-php) – Companjo Oct 03 '17 at 09:00

1 Answers1

0

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

Example:

<?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));
Neodan
  • 5,154
  • 2
  • 27
  • 38