Current Location: Home> Latest Articles> How to use PHP's array_diff_key() function to implement the "subtraction" operation in an array to remove unnecessary key-value pairs?

How to use PHP's array_diff_key() function to implement the "subtraction" operation in an array to remove unnecessary key-value pairs?

M66 2025-06-06

How to implement the "subtraction" operation in an array using PHP's array_diff_key() function to remove unnecessary key-value pairs

In PHP, the array_diff_key() function is a very useful array function that allows us to remove specified key-value pairs from an array. This function returns an array by comparing the keys of two arrays that contain key-value pairs in the first array but not in the second array.

Function prototype

 array_diff_key(array $array1, array $array2, array ...$arrays): array

Parameter explanation

  • $array1 : The array to perform the "subtraction" operation.

  • $array2 : The array that needs to be excluded will remove the elements in $array1 that have the same key as $array2 .

  • ...$arrays : Multiple arrays can be passed in to further exclude keys in multiple arrays.

Return value

This function returns a new array containing the elements in $array1 , whose keys do not exist in $array2 or other arrays.

Example: Remove unwanted key-value pairs

Suppose we have the following array from which we want to remove some unwanted key-value pairs.

 $array1 = [
    'name' => 'John',
    'age' => 25,
    'city' => 'New York',
    'country' => 'USA'
];

$array2 = [
    'age' => 0,
    'city' => ''
];

// use array_diff_key Remove array $array1 In-house 'age' and 'city' Key-value pairs
$result = array_diff_key($array1, $array2);

print_r($result);

Output:

 Array
(
    [name] => John
    [country] => USA
)

In this example, we remove the 'age' and 'city' keys from $array1 through the array_diff_key() function. This function compares the keys in $array1 and $array2 , and only those keys in $array1 that are not in $array2 .

Comparison of multiple arrays

The array_diff_key() function also supports multiple arrays as parameters. The following is an example showing how to use multiple arrays to remove key-value pairs.

 $array1 = [
    'name' => 'Alice',
    'age' => 30,
    'gender' => 'female',
    'email' => 'alice@m66.net'
];

$array2 = [
    'age' => 0,
    'gender' => ''
];

$array3 = [
    'email' => ''
];

// use array_diff_key 去除多个数组In-house键
$result = array_diff_key($array1, $array2, $array3);

print_r($result);

Output:

 Array
(
    [name] => Alice
)

In this example, we remove the 'age' , 'gender' and 'email' keys from $array1 at the same time. The function checks whether the key in $array1 is in $array2 and $array3 , and if so, the key-value pair is removed.

Things to note

  1. Keep array key consistency : array_diff_key() only compares the keys of the array, regardless of the value. If you need to compare values, you can use array_diff() or array_diff_assoc() .

  2. Empty array processing : If the passed array is empty, return to the original array because there are no elements that can be compared.

  3. Multidimensional array : The array_diff_key() function only compares the first layer key of the array. If the array is a multidimensional array, array_diff_key() must be applied layer by layer.

Conclusion

Through the array_diff_key() function, PHP provides a concise and efficient way to "subtraction" the array to remove unnecessary key-value pairs. You can use this function to fine-grained control of the array, remove unnecessary elements, and make the code more concise and maintainable.