Arrays
extends PlatformUtility
in package
Provides access to array utility functions.
Tags
Table of Contents
- castAll() : array<string|int, mixed>
- Cast all entities in array
- count() : int
- Get the length of the array.
- crossJoin() : array<string|int, mixed>
- Takes an array of arrays and produces a new array containing the cartesian product of all array values.
- diff() : array<string|int, mixed>
- Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
- empty() : bool
- Check if an array is null or empty.
- filter() : array<string|int, mixed>
- If the $callable function returns true, the current value from array is returned into the result array
- first() : mixed
- Get first value of the array.
- flatten() : array<string|int, mixed>
- Flattens a multi-dimensional array into a single-dimensional array.
- get() : mixed
- Get value from array or get get fallback if it don't exist.
- inArray() : bool
- Check if value exists in array.
- includes() : bool
- Check if value exists in array.
- isArray() : bool
- Check if variable is array.
- join() : string
- Join array values together as a string.
- keys() : array<string|int, mixed>
- Given a array, return all the keys used in the array.
- merge() : array<string|int, mixed>
- Merge arrays together.
- pluck() : array<string|int, mixed>
- Given a nested associative array ($array) and a array key ($key) this function will return a new array narrow to a list with values matching they given key in a key => value array
- pluckRecursive() : array<string|int, mixed>
- Recursively pluck values from a multi-dimensional array by a given key.
- replace() : array<string|int, mixed>
- Overwrites key values with matching keys from other arrays. (Only first level keys and their values are replaced)
- replaceRecursive() : array<string|int, mixed>
- Overwrites key values with matching keys from other arrays. (Including nested keys)
- reverse() : array<string|int, mixed>
- Reverse the ordering of array values.
- shuffle() : array<string|int, mixed>
- Randomise the ordering of array values.
- slice() : array<string|int, mixed>
- Slices values off aray and return a new array.
- unique() : array<string|int, mixed>
- Removes duplicate values from an array.
- unshift() : array<string|int, mixed>
- Prepends one or more elements to the beginning of an array and returns a new array.
- values() : array<string|int, mixed>
- Given a array, return the values
Methods
castAll()
Cast all entities in array
public
castAll(array<string|int, mixed> $array, string $to) :
array<string|int, mixed>
$to string should be one of either functions An invalid $callable will result in an empty output
boolean bool (boolean alias) double float integer int (integer alias) string str (str alias)
Parameters
- $array : array<string|int, mixed>
- $to : string
Tags
Return values
array<string|int, mixed> —count()
Get the length of the array.
public
count(array<string|int, mixed>|null $array) :
int
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
int —crossJoin()
Takes an array of arrays and produces a new array containing the cartesian product of all array values.
public
crossJoin(array<string|int, mixed> $array) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —diff()
Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
public
diff(array<string|int, mixed> $array, array<string|int, mixed> ...$arrays) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $arrays : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —empty()
Check if an array is null or empty.
public
empty(array<string|int, mixed>|null $array) :
bool
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
bool —filter()
If the $callable function returns true, the current value from array is returned into the result array
public
filter(array<string|int, mixed> $array[, string $callable = null ]) :
array<string|int, mixed>
If no callback is supplied, all empty entries of array will be removed.
$callable should be one of either functions. An invalid $callable will result in an empty output
boolval empty is_array is_bool is_double is_float is_int is_integer is_iterable is_null is_numeric is_object is_string isset strlen
Parameters
- $array : array<string|int, mixed>
- $callable : string = null
Tags
Return values
array<string|int, mixed> —first()
Get first value of the array.
public
first(array<string|int, mixed>|null $array) :
mixed
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
mixed —flatten()
Flattens a multi-dimensional array into a single-dimensional array.
public
flatten(array<string|int, mixed> $array) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —get()
Get value from array or get get fallback if it don't exist.
public
get(array<string|int, mixed>|null $array, string $key[, mixed $fallback = null ]) :
mixed
Parameters
- $array : array<string|int, mixed>|null
- $key : string
- $fallback : mixed = null
Tags
Return values
mixed —inArray()
Check if value exists in array.
public
inArray(array<string|int, mixed>|null $array, mixed $search) :
bool
This method forces strict type comparison.
Parameters
- $array : array<string|int, mixed>|null
- $search : mixed
Tags
Return values
bool —includes()
Check if value exists in array.
public
includes(array<string|int, mixed>|null $array, mixed $search) :
bool
This method always uses strict type comparison.
Parameters
- $array : array<string|int, mixed>|null
- $search : mixed
Tags
Return values
bool —isArray()
Check if variable is array.
public
isArray(mixed $array) :
bool
Parameters
- $array : mixed
Tags
Return values
bool —join()
Join array values together as a string.
public
join(array<string|int, mixed>|null $array, string $separator) :
string
Parameters
- $array : array<string|int, mixed>|null
- $separator : string
Tags
Return values
string —keys()
Given a array, return all the keys used in the array.
public
keys(array<string|int, mixed> $array) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —merge()
Merge arrays together.
public
merge(array<string|int, mixed> ...$arrays) :
array<string|int, mixed>
Parameters
- $arrays : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —pluck()
Given a nested associative array ($array) and a array key ($key) this function will return a new array narrow to a list with values matching they given key in a key => value array
public
pluck(array<string|int, mixed> $array, string $key) :
array<string|int, mixed>
This is a wrapper around PHP's native array_column function.
$data = [
["id" => 1, "title" => "Product 1"],
["id" => 2, "title" => "Product 2"],
["id" => 3, "title" => "Product 3"],
];
$productIds = $arrays->pluck($data, "id");
// Result
$productIds = [1,2,3];
Parameters
- $array : array<string|int, mixed>
- $key : string
Tags
Return values
array<string|int, mixed> —pluckRecursive()
Recursively pluck values from a multi-dimensional array by a given key.
public
pluckRecursive(array<string|int, mixed> $array, string $key) :
array<string|int, mixed>
This function will traverse through all levels of a nested array structure and extract values matching the specified key at any depth.
$data = [
["id" => 1, "title" => "Category 1", "products" => [
["id" => 10, "name" => "Product A"],
["id" => 11, "name" => "Product B"]
]],
["id" => 2, "title" => "Category 2", "products" => [
["id" => 20, "name" => "Product C"]
]]
];
$allIds = $arrays->pluckRecursive($data, "id");
// Result
$allIds = [1, 10, 11, 2, 20];
Parameters
- $array : array<string|int, mixed>
- $key : string
Tags
Return values
array<string|int, mixed> —replace()
Overwrites key values with matching keys from other arrays. (Only first level keys and their values are replaced)
public
replace(array<string|int, mixed> $array, array<string|int, mixed> ...$replacementArrays) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $replacementArrays : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —replaceRecursive()
Overwrites key values with matching keys from other arrays. (Including nested keys)
public
replaceRecursive(array<string|int, mixed> $array, array<string|int, mixed> ...$replacementArrays) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $replacementArrays : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —reverse()
Reverse the ordering of array values.
public
reverse(array<string|int, mixed>|null $array) :
array<string|int, mixed>
This functions does not mutate the original array, instead it returns a new instance of array with the same values and the ordering reversed.
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
array<string|int, mixed> —shuffle()
Randomise the ordering of array values.
public
shuffle(array<string|int, mixed>|null $array) :
array<string|int, mixed>
This functions does not mutate the original array, instead it returns a new instance of array with the same values and the ordering randomised.
Parameters
- $array : array<string|int, mixed>|null
Tags
Return values
array<string|int, mixed> —slice()
Slices values off aray and return a new array.
public
slice(array<string|int, mixed> $array, int $offset[, int|null $length = null ]) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $offset : int
- $length : int|null = null
Tags
Return values
array<string|int, mixed> —unique()
Removes duplicate values from an array.
public
unique(array<string|int, mixed> $array) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —unshift()
Prepends one or more elements to the beginning of an array and returns a new array.
public
unshift(array<string|int, mixed> $array, mixed ...$values) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $values : mixed
Tags
Return values
array<string|int, mixed> —values()
Given a array, return the values
public
values(array<string|int, mixed> $array) :
array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>