教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php数组操作实例之求数组的交集

php数组操作实例之求数组的交集

发布时间:2016-08-18   编辑:jiaochengji.com
本文介绍下,php操作数组之求数组交集的例子,主要是学习array_intersect()函数的用法,有需要的朋友参考下吧。

在php编程中,求数组的交集,一般采用php自带的函数array_intersect()。

array_intersect()函数
返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。

其形式如下:
array array_intersect(array array1,array array2[,arrayN…]) 

例子,
返回在$fruit1数组中出现的且在$fruit2和$fruit3中也出现的所有的水果:

<?php  
//求数组交集
//by www.jbxue.com
$fruit1 = array("Apple","Banana","Orange");  
$fruit2 = array("Pear","Apple","Grape");  
$fruit3 = array("Watermelon","Orange","Apple");  
$intersection = array_intersect($fruit1, $fruit2, $fruit3);  
print_r($intersection);  
  
// output  
// Array ( [0] => Apple )  
?>

注意:
只有在两个元素相等且具有相同的数据类型时,array_intersect()函数才会认为它们是相同的。

您可能感兴趣的文章:
php数组操作实例之求数组的交集
php 判断数组维数的例子(一维,二维或多维)
php数组操作实例之连接数组
php数组操作方法大全
php数组实例之php数组的其它用法
php数组入门教程之关联数组的交集
php 数组的指针操作(php数组函数汇总)
PHP extract(数组拆分)作用分析
php数组操作实例之合并数组
用go语言实现查找两个数组的异同

[关闭]
~ ~