PHP array_intersect_key Function
last modified March 13, 2025
The PHP array_intersect_key
function computes the intersection
of arrays using keys for comparison. It returns an array containing all the
entries from the first array whose keys exist in all the arguments.
Basic Definition
The array_intersect_key
function compares array keys and returns
matching key-value pairs from the first array. It preserves the original
key-value associations.
Syntax: array_intersect_key(array $array1, array ...$arrays): array
.
The function accepts multiple arrays but only compares keys, not values.
Basic array_intersect_key Example
This demonstrates a simple intersection between two arrays based on keys.
<?php $array1 = ['a' => 1, 'b' => 2, 'c' => 3]; $array2 = ['b' => 4, 'c' => 5, 'd' => 6]; $result = array_intersect_key($array1, $array2); print_r($result);
The function returns elements from $array1 where keys exist in $array2. Note that values from $array1 are preserved, not those from $array2.
Multiple Array Intersection
This example shows intersection across three arrays based on keys.
<?php $array1 = ['red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF']; $array2 = ['red' => 'Rouge', 'blue' => 'Bleu', 'yellow' => 'Jaune']; $array3 = ['red' => '赤', 'blue' => '青', 'black' => '黒']; $result = array_intersect_key($array1, $array2, $array3); print_r($result);
Only keys present in all three arrays ('red' and 'blue') are returned. The values come from the first array ($array1) in the argument list.
Numeric Key Intersection
This example demonstrates intersection with numeric array keys.
<?php $array1 = [10 => 'A', 20 => 'B', 30 => 'C']; $array2 = [20 => 'X', 30 => 'Y', 40 => 'Z']; $result = array_intersect_key($array1, $array2); print_r($result);
The function works identically with numeric keys as with string keys. Only elements with keys 20 and 30 appear in both arrays.
Preserving First Array Values
This example highlights that values always come from the first array.
<?php $userData = ['name' => 'Alice', 'age' => 25, 'email' => 'alice@example.com']; $allowedFields = ['name' => true, 'email' => true]; $filteredData = array_intersect_key($userData, $allowedFields); print_r($filteredData);
This is a common use case for filtering data. Only fields listed in $allowedFields are kept, with values from $userData preserved.
Empty Result Example
This shows what happens when there are no matching keys between arrays.
<?php $weekdays = ['Mon' => 'Monday', 'Tue' => 'Tuesday']; $weekend = ['Sat' => 'Saturday', 'Sun' => 'Sunday']; $result = array_intersect_key($weekdays, $weekend); print_r($result);
When no keys match between arrays, an empty array is returned. This is useful for cases where you need to verify no overlap exists.
Best Practices
- Key Consistency: Ensure consistent key types for reliable results.
- Order Matters: Remember values come from the first array.
- Performance: For large arrays, consider key extraction first.
- Readability: Document which array provides the values.
Source
PHP array_intersect_key Documentation
This tutorial covered the PHP array_intersect_key
function with
practical examples showing its usage for array key intersection scenarios.
Author
List all PHP Array Functions.