使用平面黑名单数组过滤二维数组的每一行
我有一个带有黑名单值的平面数组,
Array ( [0] => 1 [1] => 2 )
我需要使用该黑名单从以下二维数组中删除元素。
Array (
[0] => Array ( [id_1] => 3 [id_2] => 1 )
[1] => Array ( [id_3] => 5 [id_4] => 3 )
)
我想比较这些数组并获取一个包含二维关联数组中不存在的值的数组。
结果应为 array(3)
和 array(5,3)
。
I have a flat array with blacklisted values
Array ( [0] => 1 [1] => 2 )
I need to use that blacklist to remove elements from the following 2d array.
Array (
[0] => Array ( [id_1] => 3 [id_2] => 1 )
[1] => Array ( [id_3] => 5 [id_4] => 3 )
)
I want to compare these arrays and get an array containing the values that are not present in the 2d associative array.
The result should be array(3)
and array(5,3)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解这个问题,如果您想单独比较每个数组:
return:
否则,如果您不想循环并展平数组(使用 array_values 的 php 文档注释中的 array_flatten 函数)
< a href="http://www.php.net/manual/en/function.array-values.php#97715" rel="nofollow">http://www.php.net/manual/en/function。 array-values.php#97715
返回:
“id_4”未显示,因为它没有唯一值。您可以使用
array_values()
删除键,或者通过从代码中删除array_unique()
保留“id_4”。I'm not sure if I understand the question, if you want to compare each array individually:
returns:
Otherwise if you don't want to loop through and flatten the array (using an array_flatten function from the php doc comments of array_values)
http://www.php.net/manual/en/function.array-values.php#97715
returns:
'id_4' is not shown because it doesn't have a unique value. You can remove the keys with
array_values()
or leave in 'id_4' by removing thearray_unique()
from the code.也许是这样的?
编辑:我在您编辑问题之前输入了此内容。您仍然可以修改上面的代码,以便为关联数组中的每个子数组创建一个不同的数组。
something like this, maybe?
EDIT: I typed this before you edited your question. You can still modify the code above so that it creates a different array for each subarray in your associative array.
对于过滤行数据的函数式方法,请使用
array_map()
迭代调用array_diff()
。输出:
For a functional-style approach to filtering row data, use
array_map()
to make iterated calls ofarray_diff()
.Output: