php

How to Use “array_intersect_key()” Function in PHP

In PHP, arrays are basic data structures for storing and managing sets of values. To perform different operations on arrays, PHP has a variety of array functions that are already integrated into compilers. Among one them is the “array_intersect_key()” function, which makes array comparisons based on keys.

This article will explore the “array_intersect_key()” function in PHP.

What is “array_intersect_key()” in PHP?

In PHP, the “array_intersect_key()” is a predefined function that accepts multiple arrays as inputs and produces an array containing every key-value pair that exists in the input arrays. Instead of comparing the arrays’ values, it compares their keys to find the intersection.

Syntax

The array_intersect_key() function basic syntax is as outlined below:

array array_intersect_key($array1, $array2,...)

Parameter: The “array_intersect_key()” function requires a minimum of two array arguments. Any number of arrays larger than others can be used, as long as they are separated by commas(,) sign.

Return Value: It returns key-value pairs of an array that are present in each of the arrays used as input. Upon receiving no matching keys, it returns the NULL array.

Key-based Comparison in “array_intersect_key()” Function

Strings and integers are two different data types that can be used as array keys in PHP. The “array_intersect_key()” function matches the keys by applying string equality tests, which require that the key’s type and associated value match for there to be a connection.

Then, the “array_intersect_key()” function uses the keys of the initial array (array1) as a reference when comparing keys. It determines whether the remaining arrays, such as array2, array3, and more have these keys. The relevant key-value combination will appear in the resulting array if a key is present in each of the arrays.

Example

In the below code, first, we initialize three arrays “$array1”, “$array2”, and “$array3” having the “my_name”,”my-age”, and “subject” keys along with the values. After that, these key values are compared using the “array_intersect_key()” function. It notes that the keys “my_name” and “my_age” are shared by all three arrays. The key-value pairs related to those common keys are contained in the “$total_result” variable. Lastly, revoke the “print_r()” method to show the items inside the “$total_result” variable:

<?php

$array1 = ['my_name' => 'Anne', 'my_age' => 24, 'Subject' => 'Computer'];

$array2 = ['my_name' => 'Anne', 'my_age' => 30, 'Class' => 'English'];

$array3 = ['my_name' => 'Hazal', 'my_age' => 24, 'Subject' => 'Computer'];

$total_result = array_intersect_key($array1, $array2, $array3);

print_r($total_result);

?>

Output

Key Points

  • The “array_intersect_key()” function matches the arrays according to their keys rather than their values.
  • All input arrays’ shared key-value pairs are contained in the resultant array.
  • A blank array is presented if no similar keys are discovered.

We have briefly described the “array_intersect_key()” function in PHP.

Conclusion

In PHP, the “array_intersect_key()” function is a useful function that compares arrays depending on their keys. When users need to locate common key-value pairs among several arrays, it is quite helpful. In this guide, we have illustrated the “array_intersect_key()” function in PHP.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.