I have the following two arrays:
EDIT
On suggestion from @Wrikken I've cleaned the first array and now have this:
First Array:
Array
(
[0] => 3
[1] => 4
[2] => 9
[3] => 11
)
Second Array:
Array
(
[3] => stdClass Object ( [tid] => 3 )
[12] => stdClass Object ( tid] => 12 )
[9] => stdClass Object ( [tid] => 9 )
)
EDIT
The second array is being filtered on the first array. The second array has 3, 12, 9. The first array doesn't contain 12, so 12 should be removed from the second array.
So I should end up with:
Array
(
[3] => stdClass Object ( [tid] => 3 )
[9] => stdClass Object ( [tid] => 9 )
)
You can do this:
$keys = array_map(function($val) { return $val['value']; }, $first);
$result = array_intersect_key(array_flip($keys), $second);
The array_map
call will extract the value values from $first
so that $keys
is an array of these values. Then array_intersect_key
is used to get the intersection of $keys
(flipped to use the keys as values and vice versa) and the second array $second
.
After some clean up it was pretty clear what I needed, and this little bit sorts it out:
foreach ($second_array as $foo) {
if (!in_array($foo->tid, $first_array)) {
unset($second_array[$foo->tid]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With