Current Location: Home> Latest Articles> Will array_diff_key() retain the key order of the original array?

Will array_diff_key() retain the key order of the original array?

M66 2025-06-06

When using array_diff_key(), will the key order of the original array be preserved?

In PHP, array_diff_key() is a very useful function that can be used to compare two or more arrays, returning a new array that contains all key-value pairs in the first array, and these keys do not exist in other arrays. This allows us to remove elements from the array by keys. Next, we will explore a question: when using array_diff_key() , will the key order of the original array be preserved?

1. Basic usage of array_diff_key()

First, let's review the basic usage of array_diff_key() :

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4
];

$array2 = [
    'b' => 5,
    'd' => 6
];

$result = array_diff_key($array1, $array2);

print_r($result);
?>

Output:

 Array
(
    [a] => 1
    [c] => 3
)

In this example, array_diff_key() compares two arrays array1 and array2 and returns an array containing key-value pairs in array1 , provided that the keys do not appear in array2 . The final output is a new array, only the keys in array1 that do not appear in array2 are retained.

2. Key order of the original array

So, the question is, does array_diff_key() retain the key order of the original array? The answer is: Yes . PHP retains the order of keys in the original array when using array_diff_key() .

Let's verify this with the following code:

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4
];

$array2 = [
    'b' => 5,
    'd' => 6
];

$result = array_diff_key($array1, $array2);

echo "<pre>";
print_r($result);
echo "</pre>";
?>

Output:

 Array
(
    [a] => 1
    [c] => 3
)

Note that the new array returned by array_diff_key() still retains the order of a and c keys. Even if these keys have been compared in array2 , the key order in array1 remains unaffected. This way, we can confirm that array_diff_key() does not reorder the array, but is retained in the key order of the original array.

3. Why is the order preserved?

This is because in PHP, the order of arrays (for associative arrays) is determined by the order of insertion. When array_diff_key() is executed, PHP compares based on the existence of the keys, but does not change the order of these keys. Compared with array value operations, array_diff_key() mainly deals with keys, so keeping the key order is the design behavior of PHP.

4. Conclusion

To sum up, array_diff_key() retains the key order of the original array. Regardless of how the array is compared, the key order in the original array is preserved in the result, which allows us to rely on array_diff_key() to accurately remove elements and preserve the order of the data.

I hope this article can help you better understand the behavior of the array_diff_key() function in PHP! If you have any further questions, feel free to ask.