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.
Syntax
<code>array_intersect(array1, array2, ...)</code>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
<code>$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$result = array_intersect($array1, $array2);
print_r($result);</code>Output:
<code>Array
(
[2] => 3
[3] => 4
[4] => 5
)</code>The two arrays share the values 3, 4, and 5, which are returned in the new array.
Example 2
<code>$array1 = ['apple', 'banana', 'orange'];
$array2 = ['banana', 'grape', 'kiwi'];
$result = array_intersect($array1, $array2);
print_r($result);</code>Output:
<code>Array
(
[1] => banana
)</code>Only the value 'banana' is common to both arrays, so the result contains just that element.
Example 3
<code>$array1 = [1, 2, 3, 4, 5];
$array2 = [6, 7, 8, 9, 10];
$result = array_intersect($array1, $array2);
print_r($result);</code>Output:
<code>Array
(
)</code>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)
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.