Voting

: seven minus one?
(Example: nine)

The Note You're Voting On

caffinated
12 years ago
If you're looking for a relatively easy way to strictly intersect keys and values recursively without array key reordering, here's a simple recursive function:

<?php
function array_intersect_recursive($array1, $array2)
{
foreach(
$array1 as $key => $value)
{
if (!isset(
$array2[$key]))
{
unset(
$array1[$key]);
}
else
{
if (
is_array($array1[$key]))
{
$array1[$key] = array_intersect_recursive($array1[$key], $array2[$key]);
}
elseif (
$array2[$key] !== $value)
{
unset(
$array1[$key]);
}
}
}
return
$array1;
}
?>

<< Back to user notes page

To Top