ArrayHelper
- Full name:
\Qubus\Support\ArrayHelper
Methods
get
Gets a dot-notated key from an array, with a default value if it does not exist.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The search array. |
$key | mixed|null | The dot-notated key or array of keys. |
$default | string|null | The default value |
Throws:
set
Set an array item (dot-notated) to the value.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to insert it into |
$key | mixed | The dot-notated key to set or array of keys |
$value | mixed|null | The value |
pluck
Pluck an array of values from an array.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | Collection of arrays to pluck from. |
$key | string | Key of the value to pluck. |
$index | bool|int|string|null | Optional return array index key, true for original index. |
Return Value:
Array of plucked values.
Throws:
keyExists
Array_key_exists with a dot-notated key from an array.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The search array |
$key | mixed | The dot-notated key or array of keys |
Throws:
delete
Unsets dot-notated k?string ey from an array
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The search array |
$key | mixed|null | The dot-notated key or array of keys |
assocToKeyVal
Converts a multidimensional associative array into an array of key => values with the provided field names.
Parameters:
Parameter | Type | Description |
---|---|---|
$assoc | array|\Iterator | The array to convert. |
$keyField | string | The field name of the key field. |
$valField | string | The field name of the value field. |
Throws:
keyValToAssoc
Converts an array of key => values into a multidimensional associative array with the provided field names
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\Iterator | $array the array to convert |
$keyField | string | the field name of the key field |
$valField | string | the field name of the value field |
Throws:
toAssoc
Converts the given 1 dimensional non-associative array to an associative array.
The array given must have an even number of elements or null will be returned.
$this->toAssoc(['foo','bar']);
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | the array to change |
Return Value:
the new array or null
Throws:
isAssoc
Checks if the given array is an assoc array.
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | the array to check |
Return Value:
True if it's an assoc array, false if not.
flatten
Flattens a multi-dimensional associative array down into a 1 dimensional associative array.
public flatten(array $array, string $glue = ':', bool $reset = true, bool $indexed = true): array
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | the array to flatten |
$glue | string | what to glue the keys together with |
$reset | bool | whether to reset and start over on a new array |
$indexed | bool | whether to flatten only associative array's, or also indexed ones |
flattenAssoc
Flattens a multi-dimensional associative array down into a 1 dimensional associative array.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | the array to flatten |
$glue | string | what to glue the keys together with |
$reset | bool | whether to reset and start over on a new array |
reverseFlatten
Reverse a flattened array in its original form.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | flattened array |
$glue | string | glue used in flattening |
Return Value:
The unflattened array.
filterPrefixed
Filters an array on prefixed associative keys.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to filter. |
$prefix | string | Prefix to filter on. |
$removePrefix | bool | Whether to remove the prefix. |
filterRecursive
Recursive version of PHP's array_filter().
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to filter. |
$callback | callable|null | $callback The callback that determines whether a value is filtered. |
removePrefixed
Removes items from an array that match a key prefix.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to remove from. |
$prefix | string | Prefix to filter on. |
filterSuffixed
Filters an array on suffixed associative keys.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to filter. |
$suffix | string | Suffix to filter on. |
$removeSuffix | bool | Whether to remove the suffix. |
removeSuffixed
Removes items from an array that match a key suffix.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to remove from. |
$suffix | string | Suffix to filter on. |
filterKeys
Filters an array by an array of keys
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to filter. |
$keys | array | The keys to filter |
$remove | bool | If true, removes the matched elements. |
insert
Insert value(s) into an array, mostly an array_splice alias.
WARNING: original array is edited by reference, only bool success is returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference). |
$value | mixed | The value(s) to insert, if you want to insert an array it needs to be in an array itself. |
$pos | int | The numeric position at which to insert, negative to count from the end backwards. |
Return Value:
False when array shorter than $pos, otherwise true
insertAssoc
Insert value(s) into an array, mostly an array_splice alias WARNING: original array is edited by reference, only bool success is returned
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference) |
$values | mixed | The value(s) to insert, if you want to insert an array it needs to be in an array itself. |
$pos | int | The numeric position at which to insert, negative to count from the end backwards. |
Return Value:
false when array shorter than $pos, otherwise true
insertBeforeKey
Insert value(s) into an array before a specific key.
public insertBeforeKey(array& $original, mixed $value, int|string $key, bool $isAssoc = false): bool
WARNING: original array is edited by reference, only bool success is returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference). |
$value | mixed | The value(s) to insert, if you want to insert an array it needs to be in an array itself. |
$key | int|string | The key before which to insert. |
$isAssoc | bool | Whether the input is an associative array. |
Return Value:
False when key isn't found in the array, otherwise true.
insertAfterKey
Insert value(s) into an array after a specific key.
WARNING: original array is edited by reference, only bool success is returned.
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference). |
$value | mixed | The value(s) to insert, if you want to insert an array it needs to be in an array itself. |
$key | int|string | The key after which to insert. |
$isAssoc | bool | Whether the input is an associative array. |
Return Value:
False when key isn't found in the array, otherwise true.
insertAfterValue
Insert value(s) into an array after a specific value (first found in array).
public insertAfterValue(array& $original, mixed $value, int|string $search, bool $isAssoc = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference). |
$value | mixed | The value(s) to insert, if you want to insert an array it needs to be in an array itself. |
$search | int|string | The value after which to insert. |
$isAssoc | bool | Whether the input is an associative array. |
Return Value:
False when value isn't found in the array, otherwise true.
insertBeforeValue
Insert value(s) into an array before a specific value (first found in array)
public insertBeforeValue(array& $original, mixed $value, int|string $search, bool $isAssoc = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$original | array | The original array (by reference). |
$value | mixed | The value(s) to insert, if you want to insert an array. it needs to be in an array itself. |
$search | int|string | The value after which to insert. |
$isAssoc | bool | Whether the input is an associative array. |
Return Value:
False when value isn't found in the array, otherwise true.
sort
Sorts a multi-dimensional array by it's values.
public sort(array $array, string $key, string $order = 'asc', int $sortFlags = SORT_REGULAR): array
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to fetch from. |
$key | string | The key to sort by. |
$order | string | The order (asc or desc). |
$sortFlags | int | The php sort type flag. |
Throws:
multisort
Sorts an array on multiple values, with deep sorting support.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | Collection of arrays/objects to sort. |
$conditions | array | Sorting conditions. |
$ignoreCase | bool | Whether to sort case-insensitive. |
Throws:
average
Find the average of an array.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array containing the values. |
Return Value:
The average value.
replaceKey
Replaces key names in an array by names in the $replace parameter.
Parameters:
Parameter | Type | Description |
---|---|---|
$source | array | The array containing the key/value combinations |
$replace | array|string | Key to replace or array containing the replacement keys |
$newKey | string|null | The replacement key |
Return Value:
The array with the new keys.
merge
Merge 2 arrays recursively, differs in 2 important ways from array_merge_recursive().
When there's 2 different values and not both arrays, the latter value overwrites the earlier instead of merging both into an array.
Numeric keys that don't conflict aren't changed, only when a numeric key already exists is the value added using array_push().
Throws:
mergeAssoc
Merge 2 arrays recursively, differs in 2 important ways from array_merge_recursive().
When there's 2 different values and not both arrays, the latter value overwrites the earlier instead of merging both into an array. Numeric keys are never changed.
Throws:
prepend
Prepends a value with an associative key to an array.
Will overwrite if the value exists.
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | The array to prepend to |
$key | array|string | The key or array of keys and values |
$value | mixed|null | The value to prepend |
inArrayRecursive
Recursive in_array
Parameters:
Parameter | Type | Description |
---|---|---|
$needle | mixed | What to search for. |
$haystack | array | Array to search in. |
$strict | bool |
Return Value:
Whether the needle is found in the haystack.
isMulti
Checks if the given array is a multidimensional array.
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | The array to check |
$allKeys | bool | If true, check that all elements are arrays. |
Return Value:
True if its a multidimensional array, false if not.
search
Searches the array for a given value and returns the corresponding key or default value.
public search(array|\ArrayAccess $array, mixed $value, string|int|null $default = null, bool $recursive = true, string $delimiter = '.', bool $strict = false): string|bool|null|int
If $recursive is set to true, then the $this->search() method will return a delimiter-notated key using the $delimiter parameter.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The search array. |
$value | mixed | The searched value. |
$default | string|int|null | The default value. |
$recursive | bool | Whether to get keys recursive. |
$delimiter | string | The delimiter, when $recursive is true. |
$strict | bool | If true, do a strict key comparison. |
Throws:
unique
Returns only unique values in an array. It does not sort. First value is used.
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | The array to dedupe. |
Return Value:
array With only de-duped values.
sum
Calculate the sum of an array.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The array containing the values. |
$key | string | Key of the value to pluck. |
Return Value:
The sum value
Throws:
reindex
Returns the array with all numeric keys re-indexed, and string keys untouched.
Parameters:
Parameter | Type | Description |
---|---|---|
$arr | array | The array to reindex. |
Return Value:
Re-indexed array.
previousByKey
Get the previous value or key from an array using the current array key.
public previousByKey(array|\ArrayAccess $array, mixed $key, bool $getValue = false, bool $strict = false): string|bool|null|int
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The array containing the values. |
$key | mixed | Key of the current entry to use as reference. |
$getValue | bool | If true, return the previous value instead of the previous key. |
$strict | bool | If true, do a strict key comparison. |
Return Value:
The value in the array, null if there is no previous value, or false if the key doesn't exist.
Throws:
nextByKey
Get the next value or key from an array using the current array key.
public nextByKey(array|\ArrayAccess $array, mixed $key, bool $getValue = false, bool $strict = false): string|bool|null|int
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The array containing the values. |
$key | mixed | Key of the current entry to use as reference. |
$getValue | bool | If true, return the next value instead of the next key. |
$strict | bool | If true, do a strict key comparison. |
Return Value:
The value in the array, null if there is no next value, or false if the key doesn't exist.
Throws:
previousByValue
Get the previous value or key from an array using the current array value
public previousByValue(array|\ArrayAccess $array, mixed $value, bool $getValue = true, bool $strict = false): string|bool|null|int
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The array containing the values. |
$value | mixed | Value of the current entry to use as reference. |
$getValue | bool | If true, return the previous value instead of the previous key. |
$strict | bool | If true, do a strict key comparison. |
Return Value:
The value in the array, null if there is no previous value, or false if the key doesn't exist.
Throws:
nextByValue
Get the next value or key from an array using the current array value.
public nextByValue(array|\ArrayAccess $array, mixed $value, bool $getValue = true, bool $strict = false): string|bool|null|int
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array|\ArrayAccess | $array The array containing the values. |
$value | mixed | Value of the current entry to use as reference. |
$getValue | bool | If true, return the next value instead of the next key. |
$strict | bool | If true, do a strict key comparison. |
Return Value:
The value in the array, null if there is no next value, or false if the key doesn't exist
Throws:
subset
Return the subset of the array defined by the supplied keys.
Returns $default for missing keys, as with $this->get().
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array containing the values. |
$keys | array | List of keys (or indices) to return. |
$default | mixed|null | Value of missing keys; default null. |
Return Value:
An array containing the same set of keys provided.
Throws:
value
Takes a value and checks if it is a Closure or not, if it is it will return the result of the closure, if not, it will simply return the value.
Parameters:
Parameter | Type | Description |
---|---|---|
$var | mixed | The value to get. |
Automatically generated on 2025-10-13