PHP: array_intersect() function
PHP: Computes the intersection of arrays
The array_intersect() function is used to compare two or more arrays and returns an array containing all the values of the first array that are present in other arrays. In this operation, keys are preserved.
Version:
(PHP 4 and above)
Syntax:
array_diff (array1, array2, array3 ... )
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array1 | Reference array. | Required | Array |
array2 | Compared with the first array. | Required | Array |
array3.. | Compared with the first array | Optional | Array |
Return value:
An array containing all the keys and values in array1 if it's values exist in array2, array3 etc.
Value Type: Array
Example:
<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
$array2=array("Orange" => 100, "Apple" => 200, "Banana" => 300);
$result=array_intersect($array1, $array2);
print_r($result);
?>
Output:
Array ( [Orange] => 100 [Apple] => 200 [Banana] => 300 )
Pictorial Presentation:
View the example in the browser
Practice here online :
See also
Previous:array_intersect_ ukey
Next: array_key_exists
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php/function-reference/array_intersect.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics