Created
November 8, 2017 09:33
-
-
Save utlime/460534db1c34e8f4d5f85cc4e85de261 to your computer and use it in GitHub Desktop.
php array_intersect_key_recursive
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 | |
function array_intersect_key_recursive(array $array, array ...$arrays) | |
{ | |
$array = array_intersect_key($array, ...$arrays); | |
foreach (array_keys($array) as $key) { | |
if (is_array($array[$key])) { | |
$array[$key] = array_intersect_key_recursive( | |
$array[$key], | |
...array_map( | |
function ($value) use ($key) { | |
if (is_array($value) && is_array($value[$key])) { | |
return $value[$key]; | |
} else { | |
return []; | |
} | |
}, | |
$arrays | |
) | |
); | |
if (count($array[$key]) == 0) { | |
unset($array[$key]); | |
} | |
} | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment