Created
June 16, 2018 05:56
-
-
Save kitzelh/0e888ee760cac2ba98b29337c212f62b to your computer and use it in GitHub Desktop.
Array Replace Recursive and Clean Out Keys with Null Value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)) | |
) | |
) | |
); |
Author
kitzelh
commented
Jun 16, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment