Skip to content

Instantly share code, notes, and snippets.

@kitzelh
Created June 16, 2018 05:56
Show Gist options
  • Save kitzelh/0e888ee760cac2ba98b29337c212f62b to your computer and use it in GitHub Desktop.
Save kitzelh/0e888ee760cac2ba98b29337c212f62b to your computer and use it in GitHub Desktop.
Array Replace Recursive and Clean Out Keys with Null Value
<?php // https://3v4l.org/5fRhD
function ArrayCleaner($input) {
foreach ($input as &$value) {
if (is_array($value)) {
$value = ArrayCleaner($value);
}
}
return array_filter($input, function($item){
return $item !== null && $item !== '';
});
}
print_r( ArrayCleaner(
array_replace_recursive(
array( "a" => "apple", "b" => "banana", "c" => array("cherry" => "red", "cucumber" => "green"))
,
array("a" => "aardvark", "c" => array("calendar" => "dates", "cherry" => "bomb", "cucumber" => null))
)
)
);
@kitzelh
Copy link
Author

kitzelh commented Jun 16, 2018

Array
(
    [a] => aardvark
    [b] => banana
    [c] => Array
        (
            [cherry] => bomb
            [calendar] => dates
        )

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment