array_intersect()

本页内容

PHP array_intersect() 函数

Category:PHP Array 数组参考手册

比较两个数组的,并返回交集:


示例

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_intersect($a1,$a2);
print_r($result);
?>

定义和用法

array_intersect() 函数用于比较两个(或更多个)数组的,并返回交集。

该函数比较两个(或更多个)数组的值,并返回一个交集数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。。

语法

array_intersect(array1,array2,array3...);

参数 描述
array1 必需。与其他数组进行比较的第一个数组。
array2 必需。与第一个数组进行比较的数组。
array3,... 可选。与第一个数组进行比较的其他数组。

技术细节

返回值: 返回一个交集数组,该数组包括了所有在被比较的数组(array1)中,同时也在任何其他参数数组(array2 或 array3 等等)中的值。
PHP 版本: 4.0.1+

更多实例

实例 1

比较三个数组的,并返回交集:


示例

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_intersect($a1,$a2,$a3);
print_r($result);
?>

Category:PHP Array 数组参考手册

此页面最后编辑于2022年8月17日 (星期三) 22:10。