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
You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…
In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…
Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…
A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…
The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…
PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
The whole world of PHP programming revolves around data, be it numbers, text, or more complicated structures. Each of these…
Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…
PHP array_flip swaps keys with values. It works well when you need values as keys in fast lookups. What is…