PHP array_diff_key Function: How it Works with Examples

array_diff_key in php

PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values of the first array.

Understand the array_diff_key

The array_diff_key function compares the keys of arrays. It returns the first array without elements that exist in other arrays.

The syntax looks like this:

array_diff_key(array $array1, array $array2, array ...$arrays)
  • array1: the base array.
  • array2: one array for comparison.
  • …arrays: more arrays for comparison.

This is the correct syntax to follow.

The function checks the keys in the first array. If a key also exists in another array, the function removes that pair. The result has only keys that do not appear in other arrays.

Examples of the array_diff_key in PHP

Basic key comparison:

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

This example shows that the function removes “y” and “z” because they exist as keys in the second array. Here is the output:

Array
(
[x] => 1
)

Use with three arrays:

$a = ["id" => 10, "name" => "Ali", "role" => "admin"];
$b = ["role" => "guest"];
$c = ["id" => 20];
$result = array_diff_key($a, $b, $c);
print_r($result);

This example checks the first array against two other arrays. The result removes “id” and “role” because they exist in $b and $c. The output:

Array
(
    [name] => Ali
)

Compare numeric keys:

$a = [10 => "apple", 20 => "pear", 30 => "grape"];
$b = [20 => "banana", 40 => "orange"];
$result = array_diff_key($a, $b);
print_r($result);

This example uses numeric keys. The function removes the key 20 because it exists in the second array. The output:

Array
(
[10] => apple
[30] => grape
)

Advanced case with mixed keys:

$a = ["id" => 101, 1 => "data", "flag" => true];
$b = ["id" => 500, 1 => "info"];
$c = ["status" => "ok", "flag" => false];
$result = array_diff_key($a, $b, $c);
print_r($result);

This example combines string keys and number keys. The function removes “id”, 1, and “flag” because they exist in other arrays. The output is an empty array.

Wrapping Up

You learned what array_diff_key does and how it compares keys in arrays. Here is a quick recap:

  • It compares only keys.
  • It removes elements with keys that appear in other arrays.
  • It keeps the original values for unique keys.
  • It works with string keys and numeric keys.

FAQs

What does php array_diff_key do?

The array_diff_key() function compares array keys from two or more arrays. It returns entries from the first array whose keys are not present in the others. Example:

$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "yellow", "d" => "black");

$result = array_diff_key($array1, $array2);
print_r($result);
Output:

Array
(
    [b] => green
    [c] => blue
)

How is php array_diff_key different from array_diff?

  • array_diff() compares values of arrays.
  • array_diff_key() compares keys of arrays.
Example:

$array1 = array("a" => "red", "b" => "green");
$array2 = array("a" => "blue", "c" => "green");

print_r(array_diff($array1, $array2));
print_r(array_diff_key($array1, $array2));
Output:

Array
(
    [b] => green
)
Array
(
    [b] => green
)

Can php array_diff_key compare more than two arrays?

Yes, array_diff_key() can accept multiple arrays. The function removes all keys that exist in any other array. Example:

$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "yellow", "d" => "black");
$array3 = array("c" => "white");

$result = array_diff_key($array1, $array2, $array3);
print_r($result);
Output:

Array
(
    [b] => green
)

Similar Reads

PHP array_count_values: How it Works with Examples

Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…

How to Delete Documents in MongoDB with PHP

Deleting records in MongoDB is a common task. You might need to clear old data, remove outdated entries, or handle…

PHP Null: How to Assign and Check for Null Values

PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…

PHP Array: Accessing and Managing Elements

PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

PHP array_key_exists: How it Works with Examples

The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…

PHP mb_substr Function: How it Works with Examples

You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

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…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

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 implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

Previous Article

HTML bdi Tag: Control Text Direction in HTML

Next Article

HTML bdo Tag – Control Text Direction

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.