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.