array_intersect() / array_diff()
| Since: | PHP 4(2000) |
|---|
Functions for retrieving common elements or differences between multiple arrays. Supports both value-based and key-based comparison.
Syntax
array_intersect(array1, array2, ...); // Returns elements in the first array that are not in the other arrays. array_diff(array1, array2, ...); // Returns elements with keys common to all arrays. array_intersect_key(array1, array2, ...); // Returns elements whose keys are in the first array but not in the other arrays. array_diff_key(array1, array2, ...);
Function List
| Function | Description |
|---|---|
| array_intersect($array1, $array2, ...) | Compares array values and returns elements whose values exist in all arrays. Keys from the first array are preserved. |
| array_diff($array1, $array2, ...) | Compares array values and returns elements that exist in the first array but not in the other arrays. |
| array_intersect_key($array1, $array2, ...) | Compares array keys and returns elements from the first array whose keys are common to all arrays. |
| array_diff_key($array1, $array2, ...) | Compares array keys and returns elements from the first array whose keys do not exist in the other arrays. |
Sample Code
Value comparison (array_intersect / array_diff)
Compares two tag lists to find common elements and differences.
sample_array_intersect.php
<?php $list_a = ['item_a', 'item_b', 'item_c', 'item_d']; $list_b = ['item_c', 'item_d', 'item_e', 'item_b']; // Get elements common to both lists $common = array_intersect($list_a, $list_b); print_r($common); // Get elements in list_a that are not in list_b $only_a = array_diff($list_a, $list_b); print_r($only_a);
Running the code produces the following output:
php sample_array_intersect.php
Array
(
[1] => item_b
[2] => item_c
[3] => item_d
)
Array
(
[0] => item_a
)
Key comparison (array_intersect_key / array_diff_key)
Compares settings arrays by key to extract common and differing elements.
sample_array_intersect_key.php
<?php $old_settings = ['theme' => 'dark', 'lang' => 'ja', 'font_size' => 14]; $new_settings = ['theme' => 'light', 'lang' => 'en', 'sidebar' => true]; // Get elements with common keys (values from first array) $common_keys = array_intersect_key($old_settings, $new_settings); print_r($common_keys); // Get keys in old settings that are not in new settings $removed = array_diff_key($old_settings, $new_settings); print_r($removed); // Get keys in new settings that are not in old settings $added = array_diff_key($new_settings, $old_settings); print_r($added);
Running the code produces the following output:
php sample_array_intersect_key.php
Array
(
[theme] => dark
[lang] => ja
)
Array
(
[font_size] => 14
)
Array
(
[sidebar] => 1
)
Practical example (permission check)
Checks whether a user has all the required permissions by comparing the user's role list against the required role list.
sample_permission_check.php
<?php
$user_roles = ['read', 'write', 'delete'];
$required_roles = ['write', 'delete'];
$matched = array_intersect($required_roles, $user_roles);
if (count($matched) === count($required_roles)) {
echo 'Access granted';
} else {
echo 'Access denied';
}
Running the code produces the following output:
php sample_permission_check.php Access granted
Notes
Both array_intersect() and array_diff() compare array values, and the keys from the first array are preserved in the returned array. Since the keys may be non-contiguous, you can use array_values() to reindex if needed.
Both array_intersect_key() and array_diff_key() compare by key. The values in the returned array come from the first array — values from subsequent arrays are not used. These functions are useful for detecting configuration differences and merging data.
To compare both values and keys at the same time, array_intersect_assoc() or array_diff_assoc() can be used. To search for a specific value within an array, in_array() / array_search() is the appropriate choice.
Keys become non-contiguous
The results of array_intersect() and array_diff() preserve the original keys from the first array. This means the returned array may have gaps in its numeric keys. Use array_values() to reindex starting from zero if needed.
sample.php
<?php $all_items = ['item_a', 'item_b', 'item_c', 'item_d', 'item_e']; $excluded = ['item_a', 'item_c']; // array_diff preserves original keys $remaining = array_diff($all_items, $excluded); print_r($remaining);
Running the code produces the following output:
php sample.php
Array
(
[1] => item_b
[3] => item_d
[4] => item_e
)
Use array_values() to reindex from zero.
sample_02.php
<?php $all_items = ['item_a', 'item_b', 'item_c', 'item_d', 'item_e']; $excluded = ['item_a', 'item_c']; $remaining = array_values(array_diff($all_items, $excluded)); print_r($remaining);
Running the code produces the following output:
php sample_02.php
Array
(
[0] => item_b
[1] => item_d
[2] => item_e
)
Comparison with an empty array works normally
Passing an empty array as an argument does not cause an error — an empty array is simply returned.
sample_03.php
<?php $items = ['item_a', 'item_b', 'item_c']; $empty = []; // Intersection with an empty array is empty $result = array_intersect($items, $empty); print_r($result); $result2 = array_diff($items, $empty); print_r($result2);
Running the code produces the following output:
php sample_03.php
Array
(
)
Array
(
[0] => item_a
[1] => item_b
[2] => item_c
)
No strict type comparison is performed
array_intersect() uses string comparison by default, so the integer 1 and the string "1" are considered equal. Since array_intersect_strict() does not exist, a custom implementation using array_filter() and in_array() with strict mode is needed if type-aware comparison is required.
sample_04.php
<?php $a = [1, 2, 3]; $b = ['1', '2', '4']; // Default string comparison: 1 and "1" match $common = array_intersect($a, $b); print_r($common);
Running the code produces the following output:
php sample_04.php
Array
(
[0] => 1
[1] => 2
)
For strict type comparison, a custom implementation is needed.
sample_05.php
<?php
$a = [1, 2, 3];
$b = ['1', '2', '4'];
// Strict type comparison (array_intersect_strict does not exist)
$strict_common = array_filter($a, function($val) use ($b) {
return in_array($val, $b, true);
});
print_r($strict_common);
Running the code produces the following output:
php sample_05.php Array ( )
Practice Patterns
Permission system: matching user roles against required roles
Checks if a user has all the required permissions for an operation.
sample_06.php
<?php
function has_required_roles($user_roles, $required_roles) {
$matched = array_intersect($required_roles, $user_roles);
return count($matched) === count($required_roles);
}
$user1_roles = ['read', 'write', 'admin'];
$user2_roles = ['read'];
$required = ['write', 'admin'];
echo has_required_roles($user1_roles, $required) ? 'user1: Access granted' : 'user1: Access denied';
echo "\n";
echo has_required_roles($user2_roles, $required) ? 'user2: Access granted' : 'user2: Access denied';
Running the code produces the following output:
php sample_06.php user1: Access granted user2: Access denied
Tag filtering: matching article tags against selected tags
Filters articles by comparing their tags against the tags the user has selected.
sample_07.php
<?php
$articles = [
['title' => 'Introduction to PHP', 'tags' => ['php', 'beginner', 'tutorial']],
['title' => 'Advanced Array Functions', 'tags' => ['php', 'array', 'advanced']],
['title' => 'Database Integration Guide', 'tags' => ['database', 'php', 'tutorial']],
];
$selected_tags = ['php', 'tutorial'];
$matched_articles = array_filter($articles, function($article) use ($selected_tags) {
return count(array_intersect($article['tags'], $selected_tags)) > 0;
});
foreach ($matched_articles as $article) {
echo $article['title'] . "\n";
}
Running the code produces the following output:
php sample_07.php Introduction to PHP Advanced Array Functions Database Integration Guide
If you find any errors or copyright issues, please contact us.