PHP array_intersect() Function
Complete PHP Array Reference Manual
Example
Compare two arrays by theirValue, and return the intersection:
" red " , " b " => " green " , " c " => " blue " , " d " => " yellow " ) ; $a2 = array ( " e " => " red " , " f " => " green " , " g " => " blue " ) ; $result = array_intersect ( $a1 , $a2 ) ; print_r ( $result ) ; ?>Run Example »
Definition and Usage
The array_intersect() function compares two (or more) arrays by theirValue, and returns the intersection.
This function compares the values of two (or more) arrays and returns an intersection array containing all values in array1 that are also present in all the other argument arrays.
Syntax
array_intersect(
array1,array2,array3...
);
| Parameters | Description |
|---|---|
| array1 | Required. The first array to compare with the other arrays. |
| array2 | Required. An array to compare with the first array. |
| array3,... | Optional. Other arrays to compare with the first array. |
Technical Details
| Return Value: | Returns an intersection array containing all the keys and values in the compared array (array1) that are also present in any of the other argument arrays (array2 or array3 , etc.). |
|---|---|
| PHP Version: | 4.0.1+ |
More examples
Example 1
Compare three arrays by theirValue, and return the intersection:
" 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 ) ; ?>Run Example »
Complete PHP Array Reference Manual