English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP array_intersect() function calculates the intersection of arrays
array array_intersect ( array $array1, array $array2 , array $array3 ];
The array_intersect() function is used to compare the keys and values of two (or more) arrays and return the intersection.
The function compares the keys and values of two (or more) arrays and returns an intersection array that includes all the values in the compared arrays (array1) and also in any other parameter arrays (array2 or array3 etc.) of the keys.
Description
The array_intersect() function returns an intersection array of two or more arrays.
The result array includes all the values that are in the compared arrays, as well as in all other parameter arrays, with the key names unchanged.
Note:Only values are used for comparison.
Serial Number | Parameters and Description |
---|---|
1 | array1(Required) The first array is the array to be compared with other arrays. |
2 | array2(Required) This is the array to be compared with the first array |
3 | array3(Optional) This is an array to be compared with the first array |
Returns an intersection array that includes all the values that are in the compared arrays (array1) and also in any other parameter arrays (array2 or array3 etc.) of the keys.
Compare the keys and values of two arrays and return the intersection:
<?php $input1 = array("a" => "BMW", "Maruthi", "blue"); $input2 = array("b" => "BMW", "yellow", "Maruthi"); $result = array_intersect($input1, $input2); print_r($result); ?>Test and See‹/›
Output Result:
Array ( [a] => BMW [0] => Maruthi )