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 $_ENV: Manage Environment Variables

In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…

PHP function_exists: Avoid Function Redeclaration

The function_exists() function in PHP checks whether a given function is defined or not. The function_exists() function works for both…

PHP DOMDocument: XML DOM Parser Guide

In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…

PHP Resource Type | How the get_resource_type() Works

In this tutorial, I will explain what does mean the PHP resource and we are going to cover all PHP…

PHP break Statement: How to Exit a Loop

The PHP break statement helps in controlling the flow of loop work. It gives you a way to stop a…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

PHP Integers Guide: Types, Limits, and Conversions

You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

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.