When comparing associative arrays in PHP, sometimes matching values aren't enough. You may need to find entries where both the key and its corresponding value are identical.
The array_intersect_assoc() function does exactly that. It identifies matching key-value pairs across arrays, making it useful for comparing application settings, user records, and structured data.
What Is array_intersect_assoc() in PHP?
The array_intersect_assoc() function compares arrays and returns entries from the first array whose keys and values also appear in every comparison array.
Its syntax is:
array_intersect_assoc(
array $array,
array ...$arrays
): array
The parameters are:
$array— The first array containing the entries to check....$arrays— Additional arrays to compare against.
The function returns a new array containing matching key-value pairs. The original arrays remain unchanged.
Unlike array_intersect(), which compares values only, array_intersect_assoc() requires both the key and value to match.
Example 1: Comparing Two Associative Arrays
Let's compare two user records.
<?php
$user1 = [
"name" => "Alex",
"role" => "Editor",
"status" => "Active"
];
$user2 = [
"name" => "Alex",
"role" => "Admin",
"status" => "Active"
];
$result = array_intersect_assoc($user1, $user2);
print_r($result);
Output:
Array
(
[name] => Alex
[status] => Active
)
The name and status entries match in both arrays.
The role entry is excluded because its value differs, even though the key exists in both arrays.
Why Array Keys Matter
Two entries with identical values won't match if their keys are different.
<?php
$first = ["primary" => "PHP"];
$second = ["language" => "PHP"];
$result = array_intersect_assoc($first, $second);
var_dump($result);
Output:
array(0) {
}
Both arrays contain "PHP", but the keys are different. Therefore, no matching pair exists.
The same rule applies to indexed arrays. A value at index 0 doesn't match an identical value at index 1.
Example 2: Comparing Multiple Arrays
You can compare three or more arrays in a single call.
<?php
$settings1 = [
"theme" => "dark",
"language" => "en",
"region" => "eu"
];
$settings2 = [
"theme" => "dark",
"language" => "en",
"region" => "us"
];
$settings3 = [
"theme" => "dark",
"language" => "fr",
"region" => "eu"
];
$result = array_intersect_assoc(
$settings1,
$settings2,
$settings3
);
print_r($result);
Output:
Array
(
[theme] => dark
)
Only the theme entry has the same key and value in all three arrays.
The language and region entries are excluded because their values differ in at least one array.
Practical Example: Finding Shared Application Settings
Imagine you're managing development and production environments. Both environments use configuration arrays, and you want to identify settings that are exactly the same.
Create a file named shared-settings.php:
<?php
declare(strict_types=1);
// Production configuration.
$production = [
"site_name" => "Knowledge Hub",
"region" => "eu",
"cache" => "enabled",
"debug" => "disabled"
];
// Development configuration.
$development = [
"site_name" => "Knowledge Hub",
"region" => "eu",
"cache" => "disabled",
"debug" => "enabled"
];
// Find identical settings.
$shared = array_intersect_assoc(
$production,
$development
);
// Display matching settings.
echo "<h2>Shared Settings</h2>";
if ($shared === []) {
echo "<p>No matching settings found.</p>";
} else {
echo "<ul>";
foreach ($shared as $key => $value) {
$message = $key . ": " . $value;
echo "<li>" . htmlspecialchars(
$message,
ENT_QUOTES,
"UTF-8"
) . "</li>";
}
echo "</ul>";
}
Browser output:
Shared Settings
site_name: Knowledge Hub
region: eu
The function identifies settings that have matching keys and values in both environments.
The cache and debug entries aren't included because their values differ.
This technique is useful when comparing environment configurations, checking shared defaults, and identifying unchanged fields between records.
Important Comparison Rules
There are several behaviors to understand when using array_intersect_assoc():
- Keys and values must match: An entry is included only when both parts match in every compared array.
- Original keys are preserved: The result keeps the keys and values from the first array.
- String keys are case-sensitive:
"Name"and"name"are different keys. - Values use string-based comparison: Integer
10and string"10"are considered equal. - Empty arrays: If any compared array is empty, the intersection is empty.
- No recursive comparison: The function is intended for one-dimensional comparisons, not nested arrays.
Important: Because values are compared through their string representations, array_intersect_assoc() doesn't perform strict type-sensitive value comparisons.
When exact types matter, use a comparison approach that explicitly checks both types and values.
array_intersect_assoc() vs Related Functions
| Function | Purpose |
|---|---|
array_intersect() |
Finds matching values, ignoring keys. |
array_intersect_assoc() |
Finds matching key-value pairs. |
array_intersect_key() |
Finds matching keys, ignoring values. |
array_diff_assoc() |
Returns key-value pairs from the first array that don't match. |
Use array_intersect_assoc() when you need entries that are identical in both their key and value.
Key Takeaways
array_intersect_assoc()compares array keys and values together.- It returns matching entries from the first array.
- Every comparison array must contain the same key-value pair.
- The function preserves original array keys.
- Value comparisons use string representations rather than strict data types.
- It is particularly useful for identifying shared settings and unchanged fields.
The array_intersect_assoc() function provides a straightforward way to find matching key-value pairs in PHP arrays. It's especially helpful when comparing structured records where both field names and their values matter.
For additional details, see the official PHP array_intersect_assoc() documentation.