You are on page 1of 3

Chapter 6 Arrays

count
Number of elements to insert. Must be greater than or equal to zero.
value
Value to use for filling
Return values
Returns the filled array

array_filter
Filters elements of an array using a callback function

array_filter(array $array, ?callable $callback = null, int


$mode = 0): array

Iterates over each value in the array, passing them to the callback
function. If the callback function returns true, the current value from the
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
Passes the key as the only argument to callback instead of the value
ARRAY_FILTER_USE_BOTH

100
Chapter 6 Arrays

Passes 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

array_flip
Exchanges all keys with their associated values in an array

array_flip(array $array): array

array_flip() returns an array in flip order; in other words, keys from


the array become values and values from the array become keys.
Note that the values of array need to be valid keys, so they need to be
either an int or a string. You will get a warning 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.
Parameters
array
An array of key/value pairs to be flipped
Return values
Returns the flipped array

array_intersect_assoc
Computes the intersection of arrays with an additional index check

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

101
Chapter 6 Arrays

array_intersect_assoc() returns an array containing all the values


of an array that are present in all the arguments. Note that the keys are also
used in the comparison, unlike in array_intersect().
Parameters
array
The array with master values to check
arrays
Arrays to compare values against
Return values
Returns an associative array containing all the values in the array that
are present in all of the arguments

array_intersect_key
Computes the intersection of arrays using keys for comparison

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

array_intersect_key() returns an array containing all the entries of


an array that have keys that are present in all the arguments.
Parameters
array
The array with master keys to check
arrays
Arrays to compare keys against
Return values
Returns an associative array containing all the entries of the array that
have keys that are present in all arguments

array_intersect_uassoc
Computes the intersection of arrays with an additional index check and
compares indexes by a callback function

102

You might also like