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
.
Iterates over each value in the
array
passing them to the
callback
function. If the
callback
function returns
true
, the current value from
array
is returned into the result
array
.
Array keys are preserved, and may result in gaps if the
array
was indexed. The result
array
can be reindexed using the
array_values()
function.
Parameters
array
The array to iterate over
callback
The callback function to use
If no
callback
is supplied, all empty entries of
array
will be removed. See
empty()
for how PHP defines empty in this case.
mode
Flag determining what arguments are sent to
callback
:
ARRAY_FILTER_USE_KEY
- pass key as the only argument to
callback
instead of the value
ARRAY_FILTER_USE_BOTH
- pass both value and key as arguments to
callback
instead of the value
Default is
0
which will pass value as the only argument to
callback
instead.
Return Values
Returns the filtered array.
Changelog
Version
Description
8.0.0
callback
is nullable now.
8.0.0
If
callback
expects a parameter to be passed by reference, this function will now emit an
E_WARNING
.
Examples
Example #1
array_filter()
example
<?phpfunctionodd($var){// returns whether the input integer is oddreturn$var&1;}functioneven($var){// returns whether the input integer is evenreturn!($var&1);}$array1=['a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5];$array2=[6,7,8,9,10,11,12];echo"Odd :\n";print_r(array_filter($array1,"odd"));echo"Even:\n";print_r(array_filter($array2,"even"));?>
array_flip
—
Exchanges all keys with their associated values in an array
Description
array_flip(array$array):array
array_flip()
returns an
array
in flip order, i.e. keys from
array
become values and values from
array
become keys.
Note that the values of
array
need to be valid keys, i.e. they need to be either
int
or
string
. A warning will be emitted if a value has the wrong type, and the key/value pair in question
will not be included in the result
.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.