Content · Programming · PHP

PHP array_intersect_key() Function: Usage and Examples

Learn how to use PHP array_intersect_key() to find matching array keys, filter associative arrays, and select specific fields with practical examples.

4 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with PHP associative arrays, you may need to keep only entries whose keys exist in another array. For example, you might want to select specific user fields, compare configuration structures, or filter imported records.

The array_intersect_key() function makes this possible without writing a separate loop.

What Is array_intersect_key() in PHP?

The array_intersect_key() function compares the keys of multiple arrays and returns entries from the first array whose keys exist in every comparison array.

Its syntax is:

array_intersect_key(
    array $array,
    array ...$arrays
): array

The parameters are:

  • $array — The first array containing the entries to examine.
  • ...$arrays — Additional arrays whose keys are used for comparison.

The function returns a new array containing matching keys and their original values. It preserves the keys and element order from the first array.

Important: Only array keys are compared. Values are ignored, even if they differ.

Example 1: Finding Matching Keys

Let's compare two arrays containing application settings.

<?php

$settings = [
    "theme" => "dark",
    "language" => "en",
    "timezone" => "UTC"
];

$preferences = [
    "theme" => "light",
    "language" => "fr"
];

$result = array_intersect_key(
    $settings,
    $preferences
);

print_r($result);

Output:

Array
(
    [theme] => dark
    [language] => en
)

The keys theme and language appear in both arrays, so their entries are included.

Although their values differ, PHP keeps the values from the first array. The timezone entry is excluded because its key is missing from the second array.

Example 2: Comparing Multiple Arrays

You can compare more than two arrays. A key must appear in every array to be included.

<?php

$first = [
    "name" => "Alex",
    "email" => "[email protected]",
    "role" => "Editor"
];

$second = [
    "name" => true,
    "email" => true
];

$third = [
    "name" => 1,
    "role" => 1
];

$result = array_intersect_key(
    $first,
    $second,
    $third
);

print_r($result);

Output:

Array
(
    [name] => Alex
)

Only name exists in all three arrays. The other keys are missing from at least one comparison array.

Practical Example: Selecting Allowed Form Fields

Imagine your application receives profile information from a web form. The application should accept only specific fields and ignore unexpected ones.

You can combine array_intersect_key() with array_flip() to create a simple field allowlist.

Create a file named filter-profile.php:

<?php

declare(strict_types=1);

// Example submitted data.

$submitted = [
    "name" => "Alex",
    "email" => "[email protected]",
    "city" => "London",
    "role" => "admin"
];

// Fields users are allowed to submit.

$allowedFields = [
    "name",
    "email",
    "city"
];

// Convert allowed field names into array keys.

$allowedKeys = array_flip($allowedFields);

// Keep only permitted fields.

$profile = array_intersect_key(
    $submitted,
    $allowedKeys
);

// Display the filtered data.

echo "<h2>Profile Information</h2>";

foreach ($profile as $field => $value) {

    $message = $field . ": " . $value;

    echo "<p>" . htmlspecialchars(
        $message,
        ENT_QUOTES,
        "UTF-8"
    ) . "</p>";
}

Browser output:

Profile Information

name: Alex
email: [email protected]
city: London

The role field is excluded because it isn't on the allowlist.

This approach is useful for selecting approved fields from form submissions, API responses, and imported records.

Security note: Filtering allowed field names is only one part of input handling. You must still validate the submitted values and enforce authorization rules before saving changes.

Important Comparison Rules

Keep these behaviors in mind:

  • Keys only: The function never compares array values.
  • Case-sensitive keys: "Name" and "name" are different keys.
  • Numeric keys: Indexed arrays are compared by their indexes, not their values.
  • Original data preserved: Matching entries retain the values, keys, and order from the first array.
  • Empty arrays: If any comparison array is empty, the result is empty.
  • No recursive comparison: The function compares keys at the top level only.

PHP considers two keys equal when their string representations match. Also, PHP automatically converts certain numeric string keys to integers when creating arrays.

Starting with PHP 8.0, array_intersect_key() can be called with a single array, in which case it returns that array's entries unchanged.

array_intersect_key() vs Related Functions

Function Comparison
array_intersect() Finds matching values, ignoring keys.
array_intersect_assoc() Finds matching key-value pairs.
array_intersect_key() Finds matching keys, ignoring values.
array_diff_key() Finds keys from the first array that are absent from comparison arrays.
array_intersect_ukey() Finds matching keys using a custom comparison callback.

Use array_intersect_key() when the presence of a key matters, regardless of its associated value.

Key Takeaways

  • array_intersect_key() returns entries with keys found in every compared array.
  • Values are ignored during comparison.
  • Original keys, values, and element order are preserved.
  • It works with associative and indexed arrays.
  • Combining it with array_flip() provides a convenient way to select predefined fields.

The array_intersect_key() function is a straightforward way to filter PHP arrays by their keys. It is particularly useful for selecting approved fields, comparing data structures, and extracting specific entries from associative arrays.

For additional details, see the official PHP array_intersect_key() documentation.

Share this resourceSend this page to someone who may find it useful.