Understanding PHP's array_intersect() Function: Syntax, Parameters, Return Values, and Practical Examples

This article explains PHP's array_intersect() function, covering its syntax, required parameters, return values, usage constraints, and provides three detailed code examples that demonstrate how to find common elements across multiple arrays.

php Courses
php Courses
php Courses
Understanding PHP's array_intersect() Function: Syntax, Parameters, Return Values, and Practical Examples

Syntax

array_intersect(array1, array2, ...)

The function compares two or more arrays and returns a new array containing all values that are present in every array passed as arguments.

Parameters

array1

, array2, etc., are the arrays to be compared; you may pass any number of arrays.

Return Value

array_intersect()

returns a new array with the intersecting values. If there is no intersection, an empty array is returned.

Parameter Requirements

All values in the arrays must be strings, integers, floats, or booleans; objects, resources, or other types are not allowed.

If an array contains duplicate values, the result will contain only a single instance of each intersecting value.

The original keys of the arrays are preserved in the result.

Examples

Example 1

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$result = array_intersect($array1, $array2);
print_r($result);

Output:

Array
(
    [2] => 3
    [3] => 4
    [4] => 5
)

The two arrays share the values 3, 4, and 5, which are returned in the new array.

Example 2

$array1 = ['apple', 'banana', 'orange'];
$array2 = ['banana', 'grape', 'kiwi'];
$result = array_intersect($array1, $array2);
print_r($result);

Output:

Array
(
    [1] => banana
)

Only the value 'banana' is common to both arrays, so the result contains just that element.

Example 3

$array1 = [1, 2, 3, 4, 5];
$array2 = [6, 7, 8, 9, 10];
$result = array_intersect($array1, $array2);
print_r($result);

Output:

Array
(
)

Since the arrays have no common values, array_intersect() returns an empty array.

Notes

The number of arrays passed to array_intersect() is unlimited; the function will compare all of them.

The order of elements in the input arrays does not affect the result; only the values matter.

Summary

This article provides a comprehensive guide to PHP's array_intersect() function, illustrating its syntax, parameter rules, return behavior, and practical usage through multiple examples. By using this function, developers can efficiently compare arrays and extract their common elements, simplifying code and improving performance.

Recommended PHP Learning Resources

Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System (Limited Offer)

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.

Array Operationsphp-functionsarray_intersect
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.