Table of Content
The PHP array_intersect function finds common values in arrays and returns matches as a new array.
Understand the array_intersect in PHP
The array_intersect function compares values from two or more arrays. It checks each element and returns a new array with the matched values.
The array_intersect function takes one main array and compares it with other arrays. It checks values one by one and builds a new array with only matches.
The syntax looks like this:
array_intersect($array1, $array2, ..$arrays)$array1is the base array for comparison.$array2is the second array to compare against.- You can pass more arrays to extend the comparison.
The function scans values across arrays and builds a new array with matches.
Here is a quick example:
$first = [1, 2, 3, 4];
$second = [3, 4, 5, 6];
$result = array_intersect($first, $second);
print_r($result);The function checks each number in both arrays. It finds 3 and 4 in both arrays. The final output contains these common values.
Here is the output:
Array
(
[2] => 3
[3] => 4
)
Use the array_intersect with an Associative Array
The function also works with associative arrays. It compares values only. It does not check keys in the comparison process.
For example:
$first = ["a" => "red", "b" => "green", "c" => "blue"];
$second = ["x" => "green", "y" => "yellow", "z" => "red"];
$result = array_intersect($first, $second);
print_r($result);The function checks values in both arrays. It matches “red” and “green”. It returns them with keys from the first array.
Here is the output:
Array
(
[a] => red
[b] => green
)
Here is another example:
$first = ["k1" => "apple", "k2" => "mango", "k3" => "grape"];
$second = ["m1" => "apple", "m2" => "banana", "m3" => "grape"];
$third = ["p1" => "grape", "p2" => "apple", "p3" => "pear"];
$result = array_intersect($first, $second, $third);
print_r($result);The function compares three arrays. It finds “apple” and “grape” in all arrays. It returns them with keys from the first array.
The output:
Array
(
[k1] => apple
[k3] => grape
)
The Difference Between array_intersect and Other PHP Array Functions
The array_intersect function checks values only. Other array functions may check keys or both keys and values.
array_diffreturns values from the first array that do not exist in other arrays.array_intersect_assoccompares both keys and values to find exact matches.array_diff_assoccompares both keys and values but returns only differences.
Here is a table that shows you the key differences
| Function | Compares values | Compares keys | Returns matches | Returns differences |
|---|---|---|---|---|
| array_intersect | Yes | No | Yes | No |
| array_diff | Yes | No | No | Yes |
| array_intersect_assoc | Yes | Yes | Yes | No |
| array_diff_assoc | Yes | Yes | No | Yes |
Use array_intersect to filter arrays by common values while and the array_diff when you want unmatched values. Use the *_assoc forms when keys also matter.
Examples of the array_intersect in PHP
Simple number match:
$one = [10, 20, 30, 40];
$two = [30, 40, 50, 60];
$result = array_intersect($one, $two);
print_r($result);This shows how the function compares two numeric arrays and returns the matches. It scans both arrays and gives you [30, 40] as the final output:
Array
(
[2] => 30
[3] => 40
)
Mixed string values:
$first = ["dog", "cat", "rabbit"];
$second = ["cat", "lion", "dog"];
$third = ["horse", "dog", "cat"];
$result = array_intersect($first, $second, $third);
print_r($result);This shows how the function compares three string arrays. It checks each value step by step and finds “dog” and “cat” as the common values.
Here is the output:
Array
(
[0] => dog
[1] => cat
)
Associative array use case:
$a1 = ["k1" => "pen", "k2" => "pencil", "k3" => "eraser"];
$a2 = ["x1" => "pencil", "x2" => "marker", "x3" => "pen"];
$result = array_intersect($a1, $a2);
print_r($result);This shows how associative arrays return matched values with keys from the first array. It compares both arrays and finds “pen” and “pencil”. The output:
Array
(
[k1] => pen
[k2] => pencil
)
Advanced three-array match:
$alpha = ["red", "blue", "green", "yellow"];
$beta = ["green", "red", "purple"];
$gamma = ["red", "green", "black"];
$result = array_intersect($alpha, $beta, $gamma);
print_r($result);This shows how three arrays return only values that exist in all. The function checks step by step and finds “red” and “green” as matches.
The output:
Array
(
[0] => red
[2] => green
)
Wrapping Up
You learned how the PHP array_intersect function works and how it compares values across arrays.
Here is a quick recap:
- The function checks common values across arrays.
- It works with both simple and associative arrays.
- It ignores keys and matches values only.
- The array_intersect differs from array_diff and *_assoc functions.
- It returns a new array with values that match.
FAQs
What is PHP array_intersect used for?
<?php array_intersect() ?> function compares two or more arrays.
It returns values present in all arrays.
Example:
$a = [1, 2, 3, 4];
$b = [2, 4, 6];
$result = array_intersect($a, $b);
print_r($result);
Output:
Array
(
[1] => 2
[3] => 4
)
How does array_intersect work with associative arrays?
$a = ["id" => 101, "name" => "Ali"];
$b = ["user" => 500, "name" => "Ali"];
$result = array_intersect($a, $b);
print_r($result);
Output:
Array
(
[name] => Ali
)What is the difference between array_intersect and array_diff?
- array_intersect() returns common values between arrays.
- array_diff() returns values from the first array not in others.
$a = [1, 2, 3, 4];
$b = [2, 4];
echo "Intersect:";
print_r(array_intersect($a, $b));
echo "Diff:";
print_r(array_diff($a, $b));
Output:
// Intersect:
Array
(
[1] => 2
[3] => 4
)
// Diff:
Array
(
[0] => 1
[2] => 3
)Can PHP array_intersect handle multiple arrays?
$a = [1, 2, 3, 4];
$b = [2, 3];
$c = [3, 4, 5];
$result = array_intersect($a, $b, $c);
print_r($result);
Output:
Array
(
[2] => 3
)
Similar Reads
The increment and decrement are operators that can be used to increase or decrease the variable values which have a…
Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…
In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…
The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…
Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…
It’s very important to remember user data for each session when building web applications. This enables a high level of…
The PHP shorthand conditional operator gives a quick way to choose between two values and replaces long if-else blocks. What…
A loop in PHP helps you run tasks without repeating the same lines. In this article, you will learn how…
Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…
As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…