PHP array_intersect_key function tutorial shows how to find array key intersections in PHP. Learn array_intersect_key with practical examples.
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.
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.
This demonstrates a simple intersection between two arrays based on keys.
basic_array_intersect_key.php
<?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.
This example shows intersection across three arrays based on keys.
multiple_array_intersect.php
<?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.
This example demonstrates intersection with numeric array keys.
numeric_key_intersect.php
<?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.
This example highlights that values always come from the first array.
value_preservation.php
<?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.
This shows what happens when there are no matching keys between arrays.
no_intersection.php
<?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.
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.
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.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.