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.).
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
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
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. |
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.