Worldscope

PHP array_intersect_key() Function

Palavras-chave:

Publicado em: 04/08/2025

PHP array_intersect_key() Function: Finding Common Keys

The array_intersect_key() function in PHP is used to compute the intersection of arrays using keys for comparison. This article provides a comprehensive guide on how to effectively utilize this function, covering its purpose, usage, and underlying principles.

Fundamental Concepts / Prerequisites

To fully understand array_intersect_key(), you should have a basic understanding of PHP arrays, including their structure and how keys and values work. Familiarity with other array manipulation functions in PHP is also helpful but not strictly required. You should also know what array intersection means in the context of computer science.

Core Implementation

The following code demonstrates how to use array_intersect_key() to find the elements that have keys present in both arrays:


<?php

// Sample arrays
$array1 = [
    'a' => 'red',
    'b' => 'green',
    'c' => 'blue'
];

$array2 = [
    'a' => 'orange',
    'b' => 'yellow',
    'd' => 'purple'
];

// Use array_intersect_key to find common keys
$result = array_intersect_key($array1, $array2);

// Print the result
print_r($result);

?>

Code Explanation

First, two arrays, $array1 and $array2, are defined. $array_intersect_key() takes $array1 as its primary array. It iterates through $array1. For each element in $array1, it checks if the key exists in $array2. If the key exists in all the provided arrays (in this case, just $array2), the corresponding element from $array1 is included in the resulting array, $result. If not, it is excluded. Finally, print_r() displays the contents of the $result array. In this example, the output will be an array containing only the elements with keys 'a' and 'b' from $array1, since those are the only keys present in both arrays.

Complexity Analysis

The array_intersect_key() function generally has a time complexity of O(n*m), where 'n' is the size of the first array (the one being iterated over) and 'm' is the size of other arrays passed to the function because for each element in the first array, the keys of the other arrays have to be searched. In the best-case scenario (e.g. when the keys are indexed efficiently), the complexity might approach O(n). The space complexity is O(k), where 'k' is the number of common elements found, as it needs to store the resulting array.

Alternative Approaches

One alternative approach involves iterating through the keys of $array1 using array_keys() and then manually checking if each key exists in $array2 using array_key_exists(). If a key exists in both arrays, you would add the corresponding element from $array1 to a new result array. While this method provides more control, it's generally less efficient and requires more code compared to directly using array_intersect_key(). It's also more prone to errors due to the manual implementation.

Conclusion

The array_intersect_key() function is a powerful and efficient tool for finding common keys between multiple PHP arrays. By using this function, developers can simplify their code and improve its readability when performing array key comparisons. While alternative approaches exist, array_intersect_key() offers a concise and optimized solution for this common task.