如何比较两个数组并从一个数组中删除匹配的元素以进行下一个循环? [英] How to compare two arrays and remove matching elements from one for the next loop?

查看:23
本文介绍了如何比较两个数组并从一个数组中删除匹配的元素以进行下一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您还能如何比较两个数组($A 和 $B)并减少第一个数组中的匹配元素,以便为数组 $A 的下一个循环做准备?

How else might you compare two arrays ($A and $B )and reduce matching elements out of the first to prep for the next loop over the array $A?

$A = array(1,2,3,4,5,6,7,8);
$B = array(1,2,3,4);

$C = array_intersect($A,$B);  //equals (1,2,3,4)
$A = array_diff($A,$B);       //equals (5,6,7,8)

这是最简单的方法还是有其他方法可以使用我没有想到的其他功能?我的目标是拥有一个可以循环的数组,提取相关内容组(我已经在别处定义了这些关系),直到数组返回 false.

Is this the simplest way or is there a way to use another function that I haven't thought of? My goal is to have an array that I can loop over, pulling out groups of related content (I have defined those relationships elsewhere) until the array returns false.

推荐答案

你已经明白了.只需使用 array_diffarray_intersect.没有比这更容易的了.

You've got it. Just use array_diff or array_intersect. Doesn't get much easier than that.

例如:

$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);

来源

这篇关于如何比较两个数组并从一个数组中删除匹配的元素以进行下一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆