Skip to content

Instantly share code, notes, and snippets.

@utlime
Created November 8, 2017 09:33
Show Gist options
  • Save utlime/460534db1c34e8f4d5f85cc4e85de261 to your computer and use it in GitHub Desktop.
Save utlime/460534db1c34e8f4d5f85cc4e85de261 to your computer and use it in GitHub Desktop.
php array_intersect_key_recursive
<?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