找出PHP中两个数组之间的公共/不常见元素 [英] Finding out the common / uncommon elements between two Arrays in PHP
本文介绍了找出PHP中两个数组之间的公共/不常见元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一下我有两个数组:
Consider I have two arrays:
$friends = Array('foo', 'bar', 'alpha');
$attendees = Array('foo', 'bar');
现在我需要填充一个新数组 $ nonattendees
仅包含 $ friends
数组中的元素,而 $ attendees
不 >数组。即, $ nonattendees
数组应使用'alpha'填充。
Now I need to populate a new array $nonattendees
which contains only the elements which are in $friends
array and not in $attendees
array. i.e, $nonattendees
array should be populated with 'alpha'.
内置数组操作是否可用 PHP 以实现上述功能,还是应该为编写自己的循环?
Is there any in built array operation available in PHP to achieve the above functionality or should I write my own for
loops?
推荐答案
array_diff 似乎
$nonattendees = array_diff($friends, $attendees);
这篇关于找出PHP中两个数组之间的公共/不常见元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文