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

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 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 $_REQUEST: Learn How to Capture User Inputs

Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…

PHP array_shift: Remove the First Array Element with Examples

The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

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 Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

PHP List MongoDB Collections

Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

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…

PHP Switch | How the Switch Statement Works in PHP

The PHP switch statement is multiple conditions to only execute one block if its condition generating a true boolean value.…

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.