使用平面白名单数组从对象关联数组中删除元素
我需要使用白名单值的平面数组来过滤对象数组。
$objects = [
3 => (object) ['tid' => 3],
12 => (object) ['tid' => 12],
9 => (object) ['tid' => 9],
];
$whitelist = [3, 4, 9, 11];
白名单不包含 12,因此应删除该对象。
期望的输出:
[
3 => (object) ['tid' => 3],
9 => (object) ['tid' => 9],
]
I need to filter an array of objects using a flat array of whitelisted values.
$objects = [
3 => (object) ['tid' => 3],
12 => (object) ['tid' => 12],
9 => (object) ['tid' => 9],
];
$whitelist = [3, 4, 9, 11];
The whitelist doesn't contain 12, so that object should be removed.
Desired output:
[
3 => (object) ['tid' => 3],
9 => (object) ['tid' => 9],
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以这样做:
array_map
调用将提取值 来自$first
的值,因此$keys
是这些值的数组。然后使用array_intersect_key
来获取$keys
的交集(翻转以使用键作为值,反之亦然)和第二个数组$second
。You can do this:
The
array_map
call will extract the value values from$first
so that$keys
is an array of these values. Thenarray_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:
对于关联数组,可以使用简单的键允许列表过滤器:
将返回:
For associative arrays it can be used simple key allow-list filter:
Will return:
由于您想过滤数组(通过另一个数组中包含的所有键),您可以使用 array_filter 函数。
ARRAY_FILTER_USE_KEY
常量是函数的第三个参数,因此$key
实际上是回调中$second
数组的键。可以这样调整:Since you want to filter your array (by all keys that are contained in the other array), you may use the array_filter function.
The
ARRAY_FILTER_USE_KEY
constant is the 3rd parameter of the function so that$key
is actually the key of the$second
array in the callback. This can be adjusted:在
array_filter
中使用回调如果你的第一个数组确实像这样,你可能想要将其更改为更可用的一维数组,因此您使用简单的
in_array
作为回调的一部分:我现在只看到键 &对象 ID 是相似的:
Use a callback in
array_filter
If your first array really looks like that, you might want to alter it in a more usable one-dimensional array, so you use a simple
in_array
as part of your callback:I only now see that the keys & object-ids are alike:
要将每个对象中的
tid
列值与白名单值进行比较,无需翻转、列提取或迭代调用in_array()
。调用array_uintersect()
来比较不同深度的数组。自定义函数中的$a
和$b
可以来自任一输入数组,因此首先尝试访问对象属性,如果它不存在,那么它是安全的假设该值来自平面白名单数组。代码:(Demo)
也就是说,这个问题中的数据通过将 objectArray 的键与白名单数组值。在这种情况下,最快的方法是翻转白名单并调用 array_intersect_key() ,这比这个答案早得多。
To compare the
tid
column values in each object against the whitelist values, no flipping, column extraction, or iterated calls ofin_array()
are necessary. Callarray_uintersect()
to compare the arrays of different depths.$a
and$b
in the custom function can come from either input array, so try to access the object property first and if it doesn't exist, then it is safe to assume that the value comes from the flat, whitelist array.Code: (Demo)
That said, the data in this question lends itself to faster computation by comparing the objectArray's keys against the whitelist array values. In this context, the fastest approach will be to flip the whitelist and call
array_intersect_key()
which was demonstrated much earlier than this answer.