PHP返回不同组的结果
然后
我有一堆数据,例如 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
我将这些数据分为 2 组,例如
$groupA = range(1, 5)
$groupB = range(6, 10)
,我有 < code>$data = array(1, 4) 它将返回属于 A 组的数据。同样, $data = array(7,8)
,它会返回给我B组。
那么我怎样才能编写一个脚本让$data = array(1, 4, 6, 7)
返回 A 组和 B 组?
谢谢
all
I have a bundle of data like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
then I separate these data into 2 groups which is
$groupA = range(1, 5)
$groupB = range(6, 10)
For instance, I have $data = array(1, 4)
and it will return this belong to Group A. Likewise,$data = array(7,8)
, it will return to me Group B.
So how can I write a script to let $data = array(1, 4, 6, 7)
return me Group A and Group B?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能需要使用
array_intersect
:请注意一个空数组评估为
false
而至少包含一个元素的一个计算结果为true
。警告:如果您必须使用大量组来工作对于许多元素,您可能希望使用手动方法并在找到的第一个公共元素处停止。
array_intersect
找到所有公共元素,但您实际上并不需要它。You may want to use
array_intersect
:Note that an empty array evaluates to
false
while one with at least one element evaluates totrue
.Warning: If you have to work with a lot of groups with many elements you may want to use a manual approach and stop at the first common element found.
array_intersect
finds all the common elements and you don't really need that.你的意思是这样的吗?
Greetz,
XpertEase
Do you mean something like this?
Greetz,
XpertEase
尝试对每个组使用 array_intersect ...如果交集不为空,则意味着某些元素位于这组...
Try to use array_intersect with every group... if the intersection in not null it means that some elements are in this group...
查看
See it