过滤掉四个不同元素中与多个规则不匹配的所有行
我想从数组中删除不满足某些条件的所有元素。
例如,我有这个 2D 数组:
[
['UK', '12', 'Sus', 'N'],
['UK', '12', 'Act', 'Y'],
['SQ', '14', 'Act', 'Y'],
['CD', '12', 'Act', 'Y']
]
我想删除与此格式不匹配的所有行:
['UK' or 'CD', '12', Any Value, 'Y']
留下这个过滤后的数组:
[
['UK', '12', 'Act', 'Y'],
['CD', '12', 'Act', 'Y']
]
我该怎么做?
I would like to delete all elements from an array that don't meet some condition.
For example, I have this 2D array:
[
['UK', '12', 'Sus', 'N'],
['UK', '12', 'Act', 'Y'],
['SQ', '14', 'Act', 'Y'],
['CD', '12', 'Act', 'Y']
]
and I would like to delete all rows that don't match this format:
['UK' or 'CD', '12', Any Value, 'Y']
leaving me with this filtered array:
[
['UK', '12', 'Act', 'Y'],
['CD', '12', 'Act', 'Y']
]
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
array_filter
。它允许您通过提供回调来对每个项目执行检查。在该回调函数中,对于符合您条件的项目返回 true。array_filter
返回一个数组,其中删除了所有与您的条件不匹配的项目。例如,您的示例数组可以这样过滤:
Use
array_filter
. It allows you to perform a check on each item by providing a callback. In that callback function, return true for items that match your criteria.array_filter
returns an array with a all the items that don't match your criteria removed.For instance, your example array could be filtered like this: