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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_intersect() – Compute the Intersection of Arrays

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): array

Parameters: $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
)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPArraysphp-functionsarray_intersect
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.