Current Location: Home> Function Categories> array_intersect

array_intersect

Calculate the intersection of arrays
Name:array_intersect
Category:Array
Programming Language:php
One-line Description:Compare arrays, return intersections (compare key values ​​only).

Definition and usage

array_intersect() function is used to compare the key values ​​of two (or more) arrays and return the intersection.

The function compares the key values ​​of two (or more) arrays and returns an intersection array that includes all key values ​​in the array being compared ( array1 ) and also in any other parameter array ( array2 or array3 , etc.).

Example

Example 1

Compare the key values ​​of two arrays and return 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 ) ;
?>

Try it yourself

Example 2

Compare the key values ​​of three arrays and return 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 ) ;
?>

Try it yourself

grammar

 array_intersect ( array1 , array2 , array3 ... ) ;
parameter describe
array1 Required. The first array that is compared with other arrays.
array2 Required. The array that compares to the first array.
array3 ,... Optional. Other arrays that are compared with the first array.

illustrate

array_intersect() function returns an array of intersections of two or more arrays.

The result array contains all values ​​that appear in the array being compared and in all other parameter arrays at the same time, and the key name remains unchanged.

Note: Only values ​​are used for comparison.

Similar Functions
Popular Articles