PHP array_diff_key

Last Updated: February 10, 2022

array_diff_key: Compare the arrays based on the key of the arrays and return the difference in an array

Syntax

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

Supports (PHP 5 >= 5.1.0, PHP 7, PHP 8)

Parameters

Parameter Description
array Required. array to be compared
arrays Required. Arrays to compare against

Return value

The array is returned with elements from other arrays whose keys are not appearing in the target array (First parameter of the function)

Example

$array1 = array("One","2"=>"Two","Three",4=>"Four");
$array2 = array("One" =>"One",2=>"Two","Three");
$result = array_diff_key($array1, $array2);

Output

Array ( [0] => One [4] => Four ) 

key => value pairs are considered equal only if (string) $key1 === (string)

Test