PHP array_intersect() – Compute the Intersection of Arrays
This article explains PHP's array_intersect() function, describing its purpose, parameters, return value, and provides a complete example showing how to find common values between two arrays and display the result in PHP.
The array_intersect() function returns an array containing all the values that are present in the first array and also appear in all other arrays passed as arguments, preserving the original keys.
Signature:
array_intersect(array $array1, array $array2, array ...$arrays): arrayParameters:
$array1 – The first array to compare.
$array2 – An array to compare against the first array.
...$arrays – Additional arrays to compare.
Return value: An array containing the values that exist in $array1 and in every other array supplied.
Example:
<?php
$array1 = array("green", "red", "blue");
$array2 = array("green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>Output:
Array
(
[0] => green
[1] => red
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.