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 Strings: Types, Variables & Syntax Tips

If you start working with PHP, it won't take you that long to figure out that strings are everywhere. Besides…

PHP Ternary Operator: How to Write Short Expressions

The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…

PHP Global Variables & Superglobals: A Complete Guide

Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…

PHP fclose(): Close Access of File Manipulation

Actually, PHP has a built-in function that doesn’t get the spotlight—fclose(). It ends the file manipulation code. Let’s break down…

PHP array_fill Function: Fill Array with Same Value

You can use the array_fill function in PHP when you must fill an array with one value. You may want…

PHP array_pop: Remove and Return the Last Element of an Array

The array_pop function in PHP gives you a direct way to take the last element from an array. Understand the…

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…

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…

PHP $_SERVER: Basics and Examples

PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

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…

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.