Removing arrays whose multidimensional elements don't appear in another array?
I have a multidimensional array called $item_list
:
Array
(
[0] => Array
(
[id] => 12
[customer] => Joe Bloggs
[details] => Tex开发者_运维技巧t
)
[1] => Array
(
[id] => 13
[customer] => Fred Smith
[details] => Text
)
[2] => Array
(
[id] => 14
[customer] => John Doe
[details] => Text
)
)
I have another array from a different database (hence, no SQL solution) called $id_list
which holds the $item_list
IDs that I want to keep:
Array
(
[0] => 12
[1] => 14
)
Is there a way I can remove $item_list[1]
by comparing $item_list[1]['id']
against the elements in $id_list
without manually iterating over the $item_list
arrays using foreach()
or similar?
$keep = array(12, 14);
$data = array_filter($data, function ($a) use ($keep)
{
return in_array($a['id'], $keep);
});
Note you could array_flip
the $keep
first and use isset()
for a more optimized solution.
精彩评论