PHP array_intersect_key Function: How it Works with Examples

php array_intersect_key function

The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you need to filter arrays by keys only.

Understand the array_intersect_key Function in PHP

The array_intersect_key function compares one array against others. It checks the keys and returns the entries that exist in all arrays.

Here is the syntax:

array_intersect_key($array1, $array2, ...$arrays)
  • $array1 → the first array that you compare.
  • $array2 → the second array to check against.
  • $arrays → more arrays if you want to compare more than two.
  • Return → a new array that holds only keys that match in all arrays.

Here is a quick example:

$a = ["x" => 1, "y" => 2, "z" => 3];
$b = ["y" => 7, "z" => 8];
$result = array_intersect_key($a, $b);
print_r($result);

This returns the keys that match in both arrays. You get:

Array
(
[y] => 2
[z] => 3
)

The function checks the keys in the first array. Then it looks in the other arrays. It keeps the values only if the key exists in all arrays.

So, how to handle numeric keys in array_intersect_key?

Numeric keys also work the same way. The function matches them as exact values.

$a = [10 => "apple", 20 => "banana", 30 => "cherry"];
$b = [20 => "x", 40 => "y"];
print_r(array_intersect_key($a, $b));

This keeps the key 20 because both arrays have it. Here is the output:

Array
(
[20] => banana
)

The Difference Between array_intersect and array_intersect_key

The array_intersect checks array values and keeps values that exist in all arrays, while the array_intersect_key checks array keys. It keeps entries where the keys match.

Here are the key differences:

  • array_intersect compares values only.
  • array_intersect_key compares keys only.
  • One works with data content. The other works with array structure.

Use array_intersect_key when the structure matters. Use array_intersect when the data values matter.

Examples of array_intersect_key in PHP

Match User IDs:

$users = [101 => "Tom", 102 => "Sam", 103 => "Ana"];
$active = [102 => true, 103 => true];
$result = array_intersect_key($users, $active);
print_r($result);

This shows how you can match users by ID keys only. It keeps only the users that exist in both arrays by their ID. Here is the output:

Array
(
[102] => Sam
[103] => Ana
)

Compare Products:

$products = ["A1" => "Phone", "B1" => "Tablet", "C1" => "Laptop"];
$inStock = ["A1" => 5, "C1" => 2];
$result = array_intersect_key($products, $inStock);
print_r($result);

This shows how product keys can filter stock. It returns only products that exist in the stock array and ignores others. Here is the output:

Array
(
[A1] => Phone
[C1] => Laptop
)

Multi-Array Match:

$a = ["x" => 10, "y" => 20, "z" => 30];
$b = ["y" => 1, "z" => 2, "k" => 3];
$c = ["z" => 100, "y" => 200];
$result = array_intersect_key($a, $b, $c);
print_r($result);

This shows how the function can work with many arrays. It keeps only keys that exist across all arrays.

Here is the output:

Array
(
[y] => 20
[z] => 30
)

Numeric Key Filter:

$a = [1 => "alpha", 2 => "beta", 3 => "gamma"];
$b = [2 => "red", 3 => "blue"];
$c = [3 => "final"];
$result = array_intersect_key($a, $b, $c);
print_r($result);

This shows how numeric keys behave. It keeps only the entries where the numeric key matches in all arrays.

The output:

Array
(
[3] => gamma
)

Wrapping Up

You learned what array_intersect_key does and how it works. You also saw how it compares keys, how it handles numeric keys, and how it differs from array_intersect.

Here is a quick recap:

  • array_intersect_key compares arrays by keys.
  • It returns entries where the keys exist in all arrays.
  • It works with string keys and numeric keys.
  • array_intersect compares values instead of keys.
  • Use array_intersect_key when you need to match by structure.

FAQs

What does PHP array_intersect_key do?

The function array_intersect_key compares the keys of arrays. It returns the array with keys that exist in all compared arrays. Example:
$a = ["id" => 1, "name" => "John", "age" => 30];
$b = ["id" => 2, "age" => 25, "city" => "Paris"];

$result = array_intersect_key($a, $b);
print_r($result);
Output:
Array
(
    [id] => 1
    [age] => 30
)

How is array_intersect_key different from array_intersect?

  • array_intersect compares values of arrays.
  • array_intersect_key compares only the keys of arrays.
Example:
$a = ["id" => 1, "name" => "John"];
$b = ["id" => 1, "name" => "Tarek"];

print_r(array_intersect($a, $b));
print_r(array_intersect_key($a, $b));
Output:
Array
(
    [id] => 1
)
Array
(
    [id] => 1
    [name] => John
)

Can array_intersect_key work with numeric keys in PHP?

Yes, array_intersect_key supports numeric keys. It checks for the same numeric keys across arrays and returns matches. Example:
$a = [0 => "Apple", 1 => "Banana", 2 => "Mango"];
$b = [0 => "Orange", 2 => "Peach"];

$result = array_intersect_key($a, $b);
print_r($result);
Output:
Array
(
    [0] => Apple
    [2] => Mango
)

Similar Reads

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

PHP $_SERVER: Basics and Examples

PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

How to Remove the Last Character from a String in PHP

You may need to cut the last character from a string if it shows by mistake in PHP. That may…

Static Method in PHP: How They Work in Classes

PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…

PHP Anonymous Function: How It Works with Examples

The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

History of PHP: From PHP/FI to Modern Web Development

You use PHP every day if you build websites, but most people do not know where it came from or…

PHP strict_types: How the Strict Mode Works

If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…

PHP array_column: How to Extract Values from Arrays

The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

PHP Ternary Operator: How to Write Short Expressions

The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…

Previous Article

JavaScript math.exp: How to Calculate e^x

Next Article

Object to Primitive Conversion in JavaScript with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.