array_diff_key
Compares the keys from array against the keys from arrays and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Parameters
The array to compare from
Arrays to compare against
Return Values
Returns an array containing all the entries from array whose keys are absent from all of the other arrays.
Changelog
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Examples
Example #1 array_diff_key() example
The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.
$array1 = array( ‘blue’ => 1 , ‘red’ => 2 , ‘green’ => 3 , ‘purple’ => 4 );
$array2 = array( ‘green’ => 5 , ‘yellow’ => 7 , ‘cyan’ => 8 );
?php
var_dump ( array_diff_key ( $array1 , $array2 ));
?>
The above example will output:
array(3) < ["blue"]=>int(1) ["red"]=> int(2) ["purple"]=> int(4) >
$array1 = array( ‘blue’ => 1 , ‘red’ => 2 , ‘green’ => 3 , ‘purple’ => 4 );
$array2 = array( ‘green’ => 5 , ‘yellow’ => 7 , ‘cyan’ => 8 );
$array3 = array( ‘blue’ => 6 , ‘yellow’ => 7 , ‘mauve’ => 8 );
?php
var_dump ( array_diff_key ( $array1 , $array2 , $array3 ));
?>
The above example will output:
Notes
Note:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]); .
See Also
- array_diff() — Computes the difference of arrays
- array_udiff() — Computes the difference of arrays by using a callback function for data comparison
- array_diff_assoc() — Computes the difference of arrays with additional index check
- array_diff_uassoc() — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
- array_udiff_assoc() — Computes the difference of arrays with additional index check, compares data by a callback function
- array_udiff_uassoc() — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
- array_diff_ukey() — Computes the difference of arrays using a callback function on the keys for comparison
- array_intersect() — Computes the intersection of arrays
- array_intersect_assoc() — Computes the intersection of arrays with additional index check
- array_intersect_uassoc() — Computes the intersection of arrays with additional index check, compares indexes by a callback function
- array_intersect_key() — Computes the intersection of arrays using keys for comparison
- array_intersect_ukey() — Computes the intersection of arrays using a callback function on the keys for comparison
User Contributed Notes 11 notes
To return the unique elements (those with a key that exists only once in either array but not in both) try:
function array_unique_diff ($array1, $array2)
array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
>
Example:
$array1 = array(‘blue’ => 1, ‘red’ => 2, ‘green’ => 3, ‘purple’ => 4);
$array2 = array(‘green’ => 5, ‘blue’ => 6, ‘yellow’ => 7, ‘cyan’ => 8);
array ( ‘red’ => 2, ‘purple’ => 4, ‘yellow’ => 7, ‘cyan’ => 8, )
Improved recursive version.
/**
* @author Gajus Kuizinas
* @version 1.0.0 (2013 03 19)
*/
function array_diff_key_recursive (array $arr1 , array $arr2 ) $diff = array_diff_key ( $arr1 , $arr2 );
$intersect = array_intersect_key ( $arr1 , $arr2 );
I needed something a little different where maybe even the keys in multidimensional arrays don’t match up. Setting $assoc to false will cause only to check for missing keys, otherwise it compares values as well. This was also based on ‘2ge at 2ge dot us’ function
function n_array_diff_assoc ( $a1 , $a2 , $assoc = true ) $r = array();
if( is_array ( current ( $a1 ))):
foreach( $a1 as $k => $v ):
if(isset( $a2 [ $k ])):
$diff = n_array_diff ( $a1 [ $k ], $a2 [ $k ], $assoc );
if (!empty( $diff )):
$r [ $k ] = $diff ;
endif;
else:
$r [ $k ] = $v ;
endif;
endforeach;
else:
$r = $assoc ? array_diff_assoc ( $a1 , $a2 ) : array_diff_key ( $a1 , $a2 );
endif;
return $r ;
>
?>
Seems to be a great function, especially for n-dimensions arrays. The only problem is that I cannot find it in php 5.0.3 and 5.0.4. Does it really exist ?! 🙁
[20:27:05][maxence@conurb] ~/test2/php-5.0.4$ grep PHP_FUNCTION * -r | grep -i array_diff_key[20:27:09][maxence@conurb] ~/test2/php-5.0.4$
The recursive function suggested by ‘2ge at 2ge dot us’ will provide you with empty arrays if there’s no diff.
This variant of the function cleans up empty arrays and fixes a bug in the first suggested version. It works 100%
.
function array_diff_key_recursive ( $a1 , $a2 ) foreach( $a1 as $k => $v ) //$r[$k] = is_array($v) ? $this->array_diff_key_recursive($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
if ( is_array ( $v ))
$r [ $k ]= $this -> array_diff_key_recursive ( $a1 [ $k ], $a2 [ $k ]);
>else
$r = array_diff_key ( $a1 , $a2 );
>
The PHP4 version below works only unidirectionally. If you switch the arrays around i.e. (ar2, ar1) you get different results than (ar1, ar2).
Well, you could implement in the code something more powerfull:
array_diff_key
Compares the keys from array against the keys from arrays and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Parameters
The array to compare from
Arrays to compare against
Return Values
Returns an array containing all the entries from array whose keys are absent from all of the other arrays.
Changelog
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Examples
Example #1 array_diff_key() example
The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.
$array1 = array( ‘blue’ => 1 , ‘red’ => 2 , ‘green’ => 3 , ‘purple’ => 4 );
$array2 = array( ‘green’ => 5 , ‘yellow’ => 7 , ‘cyan’ => 8 );
?php
var_dump ( array_diff_key ( $array1 , $array2 ));
?>
The above example will output:
array(3) < ["blue"]=>int(1) ["red"]=> int(2) ["purple"]=> int(4) >
$array1 = array( ‘blue’ => 1 , ‘red’ => 2 , ‘green’ => 3 , ‘purple’ => 4 );
$array2 = array( ‘green’ => 5 , ‘yellow’ => 7 , ‘cyan’ => 8 );
$array3 = array( ‘blue’ => 6 , ‘yellow’ => 7 , ‘mauve’ => 8 );
?php
var_dump ( array_diff_key ( $array1 , $array2 , $array3 ));
?>
The above example will output:
Notes
Note:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]); .
See Also
- array_diff() — Computes the difference of arrays
- array_udiff() — Computes the difference of arrays by using a callback function for data comparison
- array_diff_assoc() — Computes the difference of arrays with additional index check
- array_diff_uassoc() — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
- array_udiff_assoc() — Computes the difference of arrays with additional index check, compares data by a callback function
- array_udiff_uassoc() — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
- array_diff_ukey() — Computes the difference of arrays using a callback function on the keys for comparison
- array_intersect() — Computes the intersection of arrays
- array_intersect_assoc() — Computes the intersection of arrays with additional index check
- array_intersect_uassoc() — Computes the intersection of arrays with additional index check, compares indexes by a callback function
- array_intersect_key() — Computes the intersection of arrays using keys for comparison
- array_intersect_ukey() — Computes the intersection of arrays using a callback function on the keys for comparison
User Contributed Notes 11 notes
To return the unique elements (those with a key that exists only once in either array but not in both) try:
function array_unique_diff ($array1, $array2)
array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
>
Example:
$array1 = array(‘blue’ => 1, ‘red’ => 2, ‘green’ => 3, ‘purple’ => 4);
$array2 = array(‘green’ => 5, ‘blue’ => 6, ‘yellow’ => 7, ‘cyan’ => 8);
array ( ‘red’ => 2, ‘purple’ => 4, ‘yellow’ => 7, ‘cyan’ => 8, )
Improved recursive version.
/**
* @author Gajus Kuizinas
* @version 1.0.0 (2013 03 19)
*/
function array_diff_key_recursive (array $arr1 , array $arr2 ) $diff = array_diff_key ( $arr1 , $arr2 );
$intersect = array_intersect_key ( $arr1 , $arr2 );
I needed something a little different where maybe even the keys in multidimensional arrays don’t match up. Setting $assoc to false will cause only to check for missing keys, otherwise it compares values as well. This was also based on ‘2ge at 2ge dot us’ function
function n_array_diff_assoc ( $a1 , $a2 , $assoc = true ) $r = array();
if( is_array ( current ( $a1 ))):
foreach( $a1 as $k => $v ):
if(isset( $a2 [ $k ])):
$diff = n_array_diff ( $a1 [ $k ], $a2 [ $k ], $assoc );
if (!empty( $diff )):
$r [ $k ] = $diff ;
endif;
else:
$r [ $k ] = $v ;
endif;
endforeach;
else:
$r = $assoc ? array_diff_assoc ( $a1 , $a2 ) : array_diff_key ( $a1 , $a2 );
endif;
return $r ;
>
?>
Seems to be a great function, especially for n-dimensions arrays. The only problem is that I cannot find it in php 5.0.3 and 5.0.4. Does it really exist ?! 🙁
[20:27:05][maxence@conurb] ~/test2/php-5.0.4$ grep PHP_FUNCTION * -r | grep -i array_diff_key[20:27:09][maxence@conurb] ~/test2/php-5.0.4$
The recursive function suggested by ‘2ge at 2ge dot us’ will provide you with empty arrays if there’s no diff.
This variant of the function cleans up empty arrays and fixes a bug in the first suggested version. It works 100%
.
function array_diff_key_recursive ( $a1 , $a2 ) foreach( $a1 as $k => $v ) //$r[$k] = is_array($v) ? $this->array_diff_key_recursive($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
if ( is_array ( $v ))
$r [ $k ]= $this -> array_diff_key_recursive ( $a1 [ $k ], $a2 [ $k ]);
>else
$r = array_diff_key ( $a1 , $a2 );
>
The PHP4 version below works only unidirectionally. If you switch the arrays around i.e. (ar2, ar1) you get different results than (ar1, ar2).
Well, you could implement in the code something more powerfull: