php array_intersect 详解
来源:互联网 发布:vb label 字体大小 编辑:程序博客网 时间:2024/06/08 11:14
数组的交集 array_intersect()
array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。其形式如下:
array array_intersect(array array1,array array2[,arrayN…])
下面这个例子将返回在fruit1数组中出现的且在fruit2和fruit3中也出现的所有的水果:
<?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);// output// Array ( [0] => Apple )?>
只有在两个元素相等且具有相同的数据类型时,array_intersect()函数才会认为它们是相同的。
关联数组的交集 array_intersect_assoc()
函数array_intersect_assoc()与array_intersect()基本相同,只不过他在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。
形式如下:
array array_intersect_assoc(array array1,array array2[,arrayN…])
下面的例子返回了出现在
<?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);// output// Array ( [red] => Apple )?>
数组的差集 array_diff()
函数array_diff()返回出现在第一个数组中但其他输入数组中没有的值。这个功能与array_intersect()相反。
array array_diff(array array1,array array2[,arrayN…])
实例如下:
<?php$fruit1 = array("Apple","Banana","Orange");$fruit2 = array("Pear","Apple","Grape");$fruit3 = array("Watermelon","Orange","Apple");$intersection = array_diff($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [1] => Banana )?>
关联数组的差集 array_diff_assoc()
函数array_diff_assoc()与array_diff()基本相同,只是它在比较时还考虑了数组的键。因此,只在第一个数组中出现而不再其他输入数组中出现的键/值对才会返回到结果数组中。其形式如下:
array array_diff_assoc(array array1,array array2[,arrayN…])
下面的例子只返回了[yellow] => Banana,因为这个特殊的键/值对出现在
<?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_diff_assoc($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [yellow] => Banana )?>
阅读全文
0 0
- php array_intersect 详解
- PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数详解
- php array_diff array_intersect用法
- array_intersect两个数组交集PHP函…
- PHP使用array_intersect()函数求数组交集
- php-Arrays 函数-array_intersect-计算数组的交集
- PHP - Manual手册 - V. Array 数组函数 - array_intersect计算数组的交集
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- [PHP]php.ini参数详解
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- org.springframework.web.client.HttpClientErrorException: 400
- ajax异步请求
- IDEA出现Error running tomcat: Unable to open debugger port (127.0.0.1:50181): java.net.BindException "
- Java 集合系列02之 Collection架构
- 响应式布局的开发基础知识
- php array_intersect 详解
- eclipse保护眼睛色设置
- 学习的途中
- CS 300 Least Even Digits 二分+数位DP
- Windows Server2012搭建Git服务器
- jdbc图书购买小栗子
- Error: Q0466E: An output file can only be specified if there is a single input file
- python学习-Day7-python帮助查看及数据类型转换
- JS继承示例