php – 如何从数组中删除不在另一个数组中的键?

前端之家收集整理的这篇文章主要介绍了php – 如何从数组中删除不在另一个数组中的键?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下两个数组:

EDIT

On suggestion from @Wrikken I’ve
cleaned the first array and now have
this:

第一阵列:

Array
(
    [0] => 3
    [1] => 4
    [2] => 9
    [3] => 11
)

第二阵列:

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.

所以我最终应该:

Array
(
    [3] => stdClass Object ( [tid] => 3 )

    [9] => stdClass Object ( [tid] => 9 )
)
你可以这样做:
$keys = array_map(function($val) { return $val['value']; },$first);
$result = array_intersect_key(array_flip($keys),$second);

array_map调用将从$first中提取值,以便$keys是这些值的数组.然后array_intersect_key用于获取$keys的交集(翻转以使用键作为值,反之亦然)和第二个数组$second.

原文链接:https://www.f2er.com/php/133745.html

猜你在找的PHP相关文章