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

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP str_replace Function: How it Works with Examples

If you find a word that does not fit and want to fix it. You can use the PHP str_replace…

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…

PHP array_combine: How to Merge Arrays as Key and Value

The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…

Check If a Row Exists in PDO

At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…

PHP trim(): How to remove Whitespace from String

Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…

PHP fopen: Understanding fopen Modes

PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…

PHP fwrite: How to Write Data to Files in PHP

actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

File Handling in PHP

File Handling in PHP is an important tool especially when you need to manage files such as reading, writing, or…

PHP is_readable: Check File Accessibility

The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…

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.