When working with PHP arrays, you may need to find values that appear in multiple lists. For example, you might want to identify shared interests, find courses available to a student, or compare product lists.
The array_intersect() function makes this easy by finding values that exist in two or more arrays.
What Is array_intersect() in PHP?
The array_intersect() function compares arrays and returns the values from the first array that also appear in every comparison array.
Its syntax is:
array_intersect(
array $array,
array ...$arrays
): array
The parameters are:
$array— The first array containing the values to check....$arrays— Additional arrays to compare against.
The function returns a new array containing the matching values. The original arrays remain unchanged.
Important: array_intersect() compares values, not keys. However, it preserves the original keys from the first array in its result.
Example 1: Finding Common Values Between Two Arrays
Let's find programming languages that appear in both lists.
<?php
$available = [
"PHP",
"Python",
"Go",
"JavaScript"
];
$selected = [
"Go",
"PHP",
"Rust"
];
$common = array_intersect(
$available,
$selected
);
print_r($common);
Output:
Array
(
[0] => PHP
[2] => Go
)
Only PHP and Go appear in both arrays.
Notice that the result preserves the indexes 0 and 2 from the first array.
If you need sequential indexes, use array_values():
$common = array_values($common);
Example 2: Comparing Multiple Arrays
You can compare more than two arrays in a single operation.
<?php
$languages = ["PHP", "Python", "Go", "Rust"];
$webLanguages = ["Python", "PHP", "JavaScript"];
$courseLanguages = ["Go", "PHP", "Python"];
$result = array_intersect(
$languages,
$webLanguages,
$courseLanguages
);
print_r($result);
Output:
Array
(
[0] => PHP
[1] => Python
)
A value must exist in every compared array to appear in the result.
Although Go appears in two arrays, it is missing from $webLanguages and is therefore excluded.
Example 3: Working with Associative Arrays
The function also works with associative arrays containing named keys.
<?php
$first = [
"backend" => "PHP",
"scripting" => "Python",
"systems" => "Go"
];
$second = [
"language1" => "Python",
"language2" => "PHP"
];
$result = array_intersect($first, $second);
print_r($result);
Output:
Array
(
[backend] => PHP
[scripting] => Python
)
Even though the array keys are different, PHP finds the matching values.
If you need to compare both keys and values, use array_intersect_assoc() instead.
Practical Example: Matching Courses with a Catalog
Imagine you're building an online learning platform. Students can submit a list of courses they want to take, but not every requested course is available.
You can use array_intersect() to find which requested courses exist in the catalog.
Create a file named available-courses.php:
<?php
declare(strict_types=1);
// Courses currently offered.
$catalog = [
"intro" => "PHP Basics",
"arrays" => "PHP Arrays",
"oop" => "PHP OOP",
"sql" => "SQL Essentials"
];
// Courses requested by a student.
$requested = [
"PHP Arrays",
"SQL Essentials",
"Docker Basics"
];
// Find available courses.
$available = array_intersect(
$catalog,
$requested
);
// Display matching courses.
echo "<h2>Available Courses</h2>";
if ($available === []) {
echo "<p>No matching courses found.</p>";
} else {
echo "<ul>";
foreach ($available as $course) {
echo "<li>" . htmlspecialchars(
$course,
ENT_QUOTES,
"UTF-8"
) . "</li>";
}
echo "</ul>";
}
Browser output:
Available Courses
PHP Arrays
SQL Essentials
The function returns the two courses found in both arrays. Docker Basics is excluded because it isn't available in the catalog.
This approach is useful for comparing course selections, product availability, supported features, and other lists that share common values.
Important Comparison Rules
There are several behaviors worth understanding:
- Case-sensitive:
"PHP"and"php"are different values. - Keys are preserved: Matching entries retain their keys from the first array.
- Duplicates are preserved: If the first array contains a matching value twice, both entries appear in the result.
- String-based comparison: Integer
10and string"10"are considered equal because their string representations match. - Empty arrays: If any compared array is empty, the result is empty.
- No recursive comparison: The function is intended for comparing values at one array level, not nested array structures.
The comparison is not type-strict. If your application needs to distinguish integers from numeric strings, use a comparison approach that explicitly checks types.
array_intersect() vs Related Functions
| Function | Purpose |
|---|---|
array_intersect() |
Finds values shared by all compared arrays. |
array_intersect_assoc() |
Finds matching key-and-value pairs. |
array_intersect_key() |
Finds matching array keys, ignoring values. |
array_diff() |
Finds values from the first array that are missing from the comparison arrays. |
Use array_intersect() when you need common values regardless of their array keys.
Key Takeaways
array_intersect()finds values shared between arrays.- When comparing multiple arrays, a value must appear in all of them.
- Array keys are ignored during comparison but preserved in the result.
- Matching duplicate entries from the first array remain in the result.
- Comparisons are case-sensitive and based on string representations.
- Use
array_values()when you need to reindex the resulting array.
The array_intersect() function provides a straightforward way to find shared values in PHP arrays. It's particularly useful when comparing lists, validating selections against available options, and identifying common elements without writing manual loops.
For more information, see the official PHP array_intersect() documentation.