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

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Neodan! This worked with a small edit to only filter the array keys with key = 0.
@Neodan and thats the reason it's bad practice
@Mike why it is a bad practice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.