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.
Table of Content
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?
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.
$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?
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
You deal with user input, file reads, or formatted strings. PHP needed a way to remove extra characters from the…
Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…
In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…
Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…
PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…
In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…
Updating documents in MongoDB with PHP involves using the updateOne or updateMany methods from the MongoDB PHP library. These methods allow for precise updates,…
You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…
let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…