PHP array_intersect () function
Examples
Comparison of two arrays of keys and returns the intersection:
<?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);
?>
$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);
?>
Running instance »
Definition and Usage
array_intersect () function is used to compare two (or more) key array, and returns the intersection.
This function compares two (or more) key array and returns an intersection of the array includes all the compared array (array1), and also in any other parameter array (array2 or array3 etc. ) of the key.
grammar
array_intersect( array1,array2,array3... );
参数 | 描述 |
---|---|
array1 | 必需。与其他数组进行比较的第一个数组。 |
array2 | 必需。与第一个数组进行比较的数组。 |
array3,... | 可选。与第一个数组进行比较的其他数组。 |
technical details
return value: | It returns an intersection array includes all the compared array (array1), and also in any other parameter array (array2 or array3 etc.) in the key. |
---|---|
PHP version: | 4.0.1+ |
More examples
Example 1
Comparison of three arrays of keys and returns the intersection:
<?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);
?>
$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);
?>
Running instance »
Complete PHP Array Reference