Current Location: Home> Function Categories> array_intersect_key

array_intersect_key

Compute the intersection of an array using key name comparison
Name:array_intersect_key
Category:Array
Programming Language:php
One-line Description:Compare arrays and return intersections (compare key names only).

Definition and usage

The array_intersect_key() function is used to compare the key names of two (or more) arrays and return the intersection.

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

Example

Example 1

Compare the key names of two arrays and return the intersection:

 <?php
$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ;
$a2 = array ( "a" => "red" , "c" => "blue" , ​​"d" => "pink" ) ;

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

Try it yourself

Example 2

Compare the key names of two index arrays and return the intersection:

 <?php
$a1 = array ( "red" , "green" , "blue" , ​​"yellow" ) ;
$a2 = array ( "red" , "green" , "blue" ) ;

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

Try it yourself

Example 3

Compare the key names of three arrays and return the intersection:

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

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

Try it yourself

grammar

 array_intersect_key ( 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_key() function uses key name comparison to calculate the intersection of an array.

array_intersect_key() returns an array containing the values ​​of all key names that appear in the array being compared and appear in all other parameter arrays at the same time.

Note: Only key names are used for comparison.

Similar Functions
Popular Articles