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 rtrim Function: Remove Trailing Characters or Whitespace

You deal with user input, file reads, or formatted strings. PHP needed a way to remove extra characters from the…

PHP Escape Characters: How to Escape Special Characters

Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…

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…

How to Insert Multiple Rows in MySQL with PHP?

Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…

PHP array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

Install PHP on Operating System: Ubuntu, Windows, and macOS

In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…

How to Update Documents in MongoDB Using PHP

Updating documents in MongoDB with PHP involves using the updateOne or updateMany methods from the MongoDB PHP library. These methods allow for precise updates,…

PHP array_merge: Combine Multiple Arrays into One

You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

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.