ArrayTool
in package
Array function class
Tags
Table of Contents
- columns() : array<string|int, mixed>
- Get values from an array or object based on the specified column key.
- delete() : array<string|int, mixed>
- Delete array keys specified by $ids. Preserves the keys of the original array.
- extract() : mixed
- Extract keys and values from an array (supports nested arrays).
- filter() : array<string|int, mixed>
- Filter array items that contain the specified search string.
- get() : mixed
- Get the value from an array based on the specified key. Return the default value if the key is not found.
- getFirstKey() : mixed|null
- Get the first key of an array or object. Returns null if no keys are found.
- getNextKey() : mixed|false
- Retrieve the key of the element following a specified key in the array.
- in_array() : bool
- Check if any value in the needle array exists in the haystack array.
- insertAfter() : array<string|int, mixed>
- Insert data into an array after the specified key. If the key is not found, insert at the end of the array.
- insertBefore() : array<string|int, mixed>
- Insert data into an array before the specified key. If the key is not found, insert at the end of the array.
- replace() : mixed
- Replace values in an array or object with the specified replacement values.
- search() : array<string|int, mixed>
- Searches an array for elements with a specific key-value pair and returns the matching elements.
- shift() : array<string|int, mixed>
- Removes an element from an array by key and returns the resulting array.
- sort() : array<string|int, mixed>
- Sorts an array of associative arrays by a specified key in ascending or descending order.
- toString() : string
- Convert a nested array or object into a string by concatenating its values with a glue.
- unserialize() : array<string|int, mixed>
- Unserialize a string and update the source array with the unserialized data.
Methods
columns()
Get values from an array or object based on the specified column key.
public
static columns(mixed $source, string $column_key[, mixed $index_key = null ]) : array<string|int, mixed>
Parameters
- $source : mixed
-
Array or object or array of objects
- $column_key : string
-
Name of the column to retrieve
- $index_key : mixed = null
-
Null to return the index of $source, string to return the index based on the specified column
Tags
Return values
array<string|int, mixed> —delete()
Delete array keys specified by $ids. Preserves the keys of the original array.
public
static delete(array<string|int, mixed> $array, mixed $ids) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $ids : mixed
-
Items to delete. Can be a single value, comma-separated values, or an array of values. eg. 1 or '1,2,3' or [1,2,3]
Tags
Return values
array<string|int, mixed> —extract()
Extract keys and values from an array (supports nested arrays).
public
static extract(array<string|int, mixed> $array, array<string|int, mixed> &$keys, array<string|int, mixed> &$values) : mixed
Parameters
- $array : array<string|int, mixed>
-
Array to extract eg. array('key1' => 'value1', 'key2' => 'value2', array('key3' => 'value3', 'key4' => 'value4'))
- $keys : array<string|int, mixed>
-
Reference to an array to store the keys eg. Array ( [0] => key1 [1] => key2 [2] => key3 [3] => key4 )
- $values : array<string|int, mixed>
-
Reference to an array to store the values eg. Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4 )
Return values
mixed —filter()
Filter array items that contain the specified search string.
public
static filter(array<string|int, mixed> $array, string $search) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
Array to filter
- $search : string
-
Search string
Tags
Return values
array<string|int, mixed> —get()
Get the value from an array based on the specified key. Return the default value if the key is not found.
public
static get(array<string|int, mixed> $array, mixed $key[, mixed $default = '' ]) : mixed
Parameters
- $array : array<string|int, mixed>
-
Array to retrieve the value from
- $key : mixed
-
Key to search for
- $default : mixed = ''
-
Default value to return if the key is not found
Tags
Return values
mixed —getFirstKey()
Get the first key of an array or object. Returns null if no keys are found.
public
static getFirstKey(mixed $source) : mixed|null
Parameters
- $source : mixed
-
Array or object
Tags
Return values
mixed|null —getNextKey()
Retrieve the key of the element following a specified key in the array.
public
static getNextKey(array<string|int, mixed> $array, mixed $key) : mixed|false
Parameters
- $array : array<string|int, mixed>
-
The input array.
- $key : mixed
-
The key to find the next key after.
Return values
mixed|false —The key of the element following the specified key, or false if not found.
in_array()
Check if any value in the needle array exists in the haystack array.
public
static in_array(mixed $needle, array<string|int, mixed> $haystack[, bool $strict = false ]) : bool
Parameters
- $needle : mixed
-
Value or array of values to search for
- $haystack : array<string|int, mixed>
-
Array to search in
- $strict : bool = false
-
(Optional) Perform strict comparison when checking values
Tags
Return values
bool —True if any value is found, false otherwise
insertAfter()
Insert data into an array after the specified key. If the key is not found, insert at the end of the array.
public
static insertAfter(array<string|int, mixed> $source, mixed $find, mixed $key, mixed $value) : array<string|int, mixed>
Parameters
- $source : array<string|int, mixed>
-
Array to insert into
- $find : mixed
-
Key to search for
- $key : mixed
-
Key of the data to insert
- $value : mixed
-
Data to insert
Tags
Return values
array<string|int, mixed> —insertBefore()
Insert data into an array before the specified key. If the key is not found, insert at the end of the array.
public
static insertBefore(array<string|int, mixed> $source, mixed $find, mixed $key, mixed $value) : array<string|int, mixed>
Parameters
- $source : array<string|int, mixed>
-
Array to insert into
- $find : mixed
-
Key to search for
- $key : mixed
-
Key of the data to insert
- $value : mixed
-
Data to insert
Tags
Return values
array<string|int, mixed> —replace()
Replace values in an array or object with the specified replacement values.
public
static replace(mixed $source, array<string|int, mixed> $replace) : mixed
Parameters
- $source : mixed
-
Array or object to replace values in
- $replace : array<string|int, mixed>
-
Associative array of replacement values (key => value)
Tags
Return values
mixed —The modified source array or object
search()
Searches an array for elements with a specific key-value pair and returns the matching elements.
public
static search(array<string|int, mixed> $array, string $key, mixed $search) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
The array to search
- $key : string
-
The key to search for
- $search : mixed
-
The value to search for
Tags
Return values
array<string|int, mixed> —The matching elements
shift()
Removes an element from an array by key and returns the resulting array.
public
static shift(array<string|int, mixed> $source, mixed $key) : array<string|int, mixed>
Parameters
- $source : array<string|int, mixed>
-
The source array
- $key : mixed
-
The key of the element to remove
Tags
Return values
array<string|int, mixed> —The resulting array after removing the element
sort()
Sorts an array of associative arrays by a specified key in ascending or descending order.
public
static sort(array<string|int, mixed> $array[, string $sort_key = 'id' ][, bool $sort_desc = false ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
The array to sort
- $sort_key : string = 'id'
-
The key to sort the array by
- $sort_desc : bool = false
-
Whether to sort the array in descending order
Tags
Return values
array<string|int, mixed> —The sorted array
toString()
Convert a nested array or object into a string by concatenating its values with a glue.
public
static toString(string $glue, array<string|int, mixed>|object $source) : string
Parameters
- $glue : string
-
The glue to join the values with
- $source : array<string|int, mixed>|object
-
The source array or object
Tags
Return values
string —The concatenated string
unserialize()
Unserialize a string and update the source array with the unserialized data.
public
static unserialize(string $str[, array<string|int, mixed> $source = [] ][, bool $replace = true ]) : array<string|int, mixed>
Parameters
- $str : string
-
The serialized string
- $source : array<string|int, mixed> = []
-
(Optional) Array to update with unserialized data
- $replace : bool = true
-
(Optional) Whether to replace existing values in the source array
Tags
Return values
array<string|int, mixed> —The updated source array