Compares
array
against
arrays
and returns the difference. Unlike
array_diff()
the array keys are also used in the comparison.
Parameters
array
The array to compare from
arrays
Arrays to compare against
Return Values
Returns an
array
containing all the values from
array
that are not present in any 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_assoc()
example
In this example you see the
"a" => "green"
pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair
0 => "red"
is in the output because in the second argument
"red"
has key which is
1
.
Two values from
key => value
pairs are considered equal only if
(string) $elem1 === (string)
$elem2
. In other words a strict check takes place so the string representations must be the same.
Note
:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example,
array_diff_assoc($array1[0], $array2[0]);
.
Note
:
Ensure you pass arguments in the correct order when comparing similar arrays with more keys. The new array should be the first in the list.
See Also
array_diff() - Computes the difference of arrays
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_intersect() - Computes the intersection of arrays
array_intersect_assoc() - Computes the intersection of arrays with additional index check
PHP / array_diff_key — DevDocs
array_diff_key
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_diff_key
—
Computes the difference of arrays using keys for comparison
Description
array_diff_key(array$array,array...$arrays):array
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
array
The array to compare from
arrays
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.
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
Compares
array
against
arrays
and returns the difference. Unlike
array_diff()
the array keys are used in the comparison.
Unlike
array_diff_assoc()
a user supplied callback function is used for the indices comparison, not internal function.
Parameters
array
The array to compare from
arrays
Arrays to compare against
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed$a,mixed$b):int
Return Values
Returns an
array
containing all the entries from
array
that are not present in any of the other arrays.
Examples
Example #1
array_diff_uassoc()
example
The
"a" => "green"
pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair
0 => "red"
is in the output because in the second argument
"red"
has key which is
1
.
The equality of 2 indices is checked by the user supplied callback function.
Notes
Note
:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example,
array_diff_uassoc($array1[0], $array2[0], "key_compare_func");
.
See Also
array_diff() - Computes the difference of arrays
array_diff_assoc() - Computes the difference of arrays with additional index check
array_udiff() - Computes the difference of arrays by using a callback function for data comparison
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_intersect() - Computes the intersection of arrays
array_intersect_assoc() - Computes the intersection of arrays with additional index check
array_uintersect() - Computes the intersection of arrays, compares data by a callback function
array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
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.
Unlike
array_diff_key()
a user supplied callback function is used for the indices comparison, not internal function.
Parameters
array
The array to compare from
arrays
Arrays to compare against
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed$a,mixed$b):int
Return Values
Returns an
array
containing all the entries from
array
that are not present in any of the other arrays.
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using
array_diff_ukey($array1[0], $array2[0], 'callback_func');
.
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_key() - Computes the difference of arrays using 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
Fills an array with
count
entries of the value of the
value
parameter, keys starting at the
start_index
parameter.
Parameters
start_index
The first index of the returned array.
If
start_index
is negative, the first index of the returned array will be
start_index
and the following indices will start from zero prior to PHP 8.0.0; as of PHP 8.0.0, negative keys are incremented normally (see example).
count
Number of elements to insert. Must be greater than or equal to zero, and less than or equal to
2147483647
.
value
Value to use for filling
Return Values
Returns the filled array
Errors/Exceptions
Throws a
ValueError
if
count
is out of range.
Changelog
Version
Description
8.0.0
array_fill()
now throws a
ValueError
if
count
is out of range; previously
E_WARNING
was raised, and the function returned
false
.