PHP使用array_intersect()函数求数组交集
在PHP中求数组的交集,我们可以与PHP给我们提供的现成函数:array_intersect(),其用法格式为:
array array_intersect(array array1,array array2[,arrayN…])
根据上述的语法格式,我们来写一个例子:
<?php
$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_intersect($fruit1, $fruit2, $fruit3);
print_r($intersection);
// 输出结果:
// Array ( [0] => Apple )
?>
本例子将返回在$fruit1数组中出现且在$fruit2和$fruit3中也出现的所有水果的名子。
使用array_intersect()函数时要注意:只有在两个元素相等且具有相同的数据类型时,array_intersect()函数才会认为它们是相同的,否则不能进行交集计算。array_intersect()函数返回一个保留了键的数组,只由第一个数组中出现的且在其它数组中都出现的值组成。
若要求关联数组的交集,请使用array_intersect_assoc()函数,给你个简单的例子:
<?php
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// 输出:
// Array ( [red] => Apple )
?>
array_intersect_assoc()函数语法格式如下:
array array_intersect_assoc(array array1,array array2[,arrayN…])
array_intersect_assoc()与array_intersect()基本相同,只不过他在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。
PHP使用array_intersect()函数求数组交集的更多相关文章
- [leetcode350]Intersection of Two Arrays II求数组交集
List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = ...
- 求数组差/交集函数-php数组函数(二)
求数组差集函数 函数只检查了多维数组中的一维.可以用 array_diff($array1[0], $array2[0]) 检查更深的维度. u:自定义函数比较,a(association):同时比较 ...
- JavaScript求数组中元素的最大值
要求: 求数组[2,6,1,77,52,25,7]中的最大值. 实现思路: 声明一个保存最大元素的变量 max 默认最大值max定义为数组中的第一个元素arr[0] 遍历这个数组,把里面每个数组元素和 ...
- PHP:第四章——PHP数组array_intersect计算数组交集
<pre> <?php //array_intersect计算数组交集 header("Content-Type:text/html;charset=utf-8" ...
- 利用Underscore求数组的交集、并集和差集
1 数组交集函数——intersection 数组的交集是指包含多个数组中的共同元素的一个数组,求数组的交集就是找出给定数组中的共有元素. 下面实现一个求两个数组交集的函数. 判断数组是够包含指定值, ...
- C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度
在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...
- 求数组的最小数、最大值,求一组数的平均数,sort函数详解,类数组转数组
求数组的最小值和最大值 //求数组当中最大值和最小值 var arr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30]; //第一种方法 根据排序方法来求最大值和最小值 ...
- 编写函数求整形数组a中存储的m个不重复的整数的第k大的整数(其中m>=1,1<=k<=m)很简单的一个思路是酱紫的:管他辣么多干啥,上来一把排序然后直接得答案
/** * @author:(LiberHome) * @date:Created in 2019/2/28 20:38 * @description: * @version:$ *//*编写函数求整 ...
- PHP函数和数组
所有代码可以在https://www.github.com/lozybean/learn_www中查看 PHP函数 PHP中还是有很多函数式编程的影子,函数功能还是比较强大的. 1. 函数用funct ...
随机推荐
- ratingbar设置不可调节星星数量
<RatingBar android:id="@+id/rb_bar" android:layout_width="wrap_content" andro ...
- linux下libreoffice安装测试
一.Linux安装 wget http://download.documentfoundation.org/libreoffice/stable/4.4.6/rpm/x86_64/LibreOffic ...
- java的property
System.currentTimeMillis() 返回以毫秒为单位的当前时间.System.gc() 垃圾回收System.getProperties().返回当前的系统属性System.getP ...
- Android----paint触摸轨迹监听
paint触摸轨迹监听,主要是三种而已,ACTION_DOWN,ACTION_MOVE,ACTION_UP public boolean onTouchEvent(MotionEvent event) ...
- HDU 1203 I NEED A OFFER! 01背包 概率运算预处理。
题目大意:中问题就不说了 ^—^~ 题目思路:从题目来看是很明显的01背包问题,被录取的概率记为v[],申请费用记为w[].但是我们可以预先做个处理,使问题解决起来更方便:v[]数组保留不被录取的概率 ...
- UIView 视图切换
UIView之间常用视图之间切换方式 转载自:http://www.jianshu.com/p/0d53f9402c07 在平时编写代码的过程中,页面之间的跳转可以说就和MVC模式一样是开发必须的.但 ...
- php查找文件内容
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><?ph ...
- JS学习随手笔记
===================字符串===================== 字符串中间的空格也是要算长度的. 每一个字符都可以返回对应的Unicode 编码 文字的简单加密都是通过char ...
- PAT (Advanced Level) 1084. Broken Keyboard (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- MediaScanner
http://blog.csdn.net/hellofeiya/article/details/8255898 http://www.cnblogs.com/halzhang/archive/2011 ...