arrays
Table of Contents
Classes
- CleanFlag
- Enumeration representing cleaning modes as bit flags.
Functions
- clean() : array<string|int, mixed>
- Cleans an array by removing unwanted values based on bitwise flags from the `CleanFlag` enum.
- compress() : array<string|int, mixed>
- Compresses the given array by removing values that match one or more conditions.
- deepMerge() : array<string|int, mixed>
- Recursively merges multiple arrays.
- delete() : mixed
- Unset a key or nested key in an array using dot notation.
- ensureArrayPath() : array<string|int, mixed>
- Ensures that a given key in an array contains a sub-array, and returns it by reference.
- exists() : bool
- Checks whether a key exists in an array or an object implementing ArrayAccess.
- flatten() : array<string|int, mixed>
- Flattens a nested array into a single-level array.
- get() : mixed
- Retrieves a value from an associative array using a key path.
- getFirstKey() : mixed
- Returns the first key from the provided list of keys that exists in the given array.
- getFirstValue() : mixed
- Returns the value of the first key from the provided list of keys that exists in the given array.
- hasIntKeys() : bool
- Indicates if all the keys in an array are integers.
- hasStringKeys() : bool
- Indicates if all the keys in an array are strings.
- inBetween() : array<string|int, mixed>
- Insert an element between all the items in an array.
- isAssociative() : bool
- Determines if an array is associative.
- isIndexed() : bool
- Determines if an array is indexed (i.e., has sequential integer keys starting at 0).
- removeKeys() : array<string|int, mixed>
- Removes a set of keys from an array.
- set() : array<string|int, mixed>
- Sets a value in an associative array using a key path.
- setArrayValue() : array<string|int, mixed>
- Sets a value in a flat associative array using a single key.
- shuffle() : array<string|int, mixed>
- Shuffles the elements of an array in place using the Fisher-Yates algorithm.
- stub() : array<string|int, mixed>
- Returns a new empty array.
- swap() : array<string|int, mixed>
- Swaps two indexed values in a specific array representation.
- tail() : array<string|int, mixed>
- Returns a new array containing all elements except the first one.
- toArray() : array<string|int, mixed>
- Ensures the given value is returned as an array.
- unique() : array<string|int, mixed>
- Removes duplicate values from an array and reindexes it.
Functions
clean()
Cleans an array by removing unwanted values based on bitwise flags from the `CleanFlag` enum.
clean([array<string|int, mixed> $array = [] ][, int $flags = CleanFlag::DEFAULT ]) : array<string|int, mixed>
This function allows flexible and recursive filtering of arrays. Cleaning behavior is fully customizable via bitwise flags.
Supported flags from CleanFlag
:
CleanFlag::NULLS
: Removesnull
values.CleanFlag::EMPTY
: Removes empty strings (''
).CleanFlag::TRIM
: Trims strings and treats whitespace-only strings as empty.CleanFlag::EMPTY_ARR
: Removes empty arrays (after recursive cleaning).CleanFlag::RECURSIVE
: Cleans nested arrays recursively.CleanFlag::FALSY
: Removes all PHP falsy values (null
,''
,0
,0.0
,'0'
,false
,[]
).CleanFlag::MAIN
: Shortcut for enabling all the main flags:NULLS | EMPTY | EMPTY_ARR | TRIM
.
Default behavior
Using CleanFlag::DEFAULT
is equivalent to enabling:
CleanFlag::NULLS | CleanFlag::EMPTY | CleanFlag::TRIM | CleanFlag::EMPTY_ARR | CleanFlag::RECURSIVE
That means by default, clean()
removes:
null
- empty strings
- strings containing only spaces/tabs/newlines
- empty arrays
- and also cleans nested arrays recursively.
Parameters
- $array : array<string|int, mixed> = []
-
The input array to clean. Nested arrays are processed only if
CleanFlag::RECURSIVE
is set. - $flags : int = CleanFlag::DEFAULT
-
A bitmask of
CleanFlag
values. Defaults toCleanFlag::DEFAULT
.
Tags
Return values
array<string|int, mixed> —The filtered array:
- Associative arrays: keys are preserved.
- Indexed arrays: automatically reindexed to remove numeric gaps.
compress()
Compresses the given array by removing values that match one or more conditions.
compress(array<string|int, mixed> $array[, array<string|int, mixed>|null $options = [] ][, int $currentDepth = 0 ]) : array<string|int, mixed>
Useful for cleaning up associative arrays (e.g., from form submissions or object exports) by removing nulls, empty strings, or other unwanted values. Supports recursion into nested arrays or objects.
Parameters
- $array : array<string|int, mixed>
-
The input array to compress.
- $options : array<string|int, mixed>|null = []
-
Optional configuration:
- clone (bool) If
true
, works on a cloned copy of the array. Original remains unchanged. (default: false) - conditions (callable|array
) One or more callbacks: (mixed $value): bool
. If any condition returnstrue
, the value is removed. ( default:fn($v) => is_null($v)
)* - excludes (string[]) List of keys to exclude from filtering, even if matched by a condition.
- recursive (bool) Whether to recursively compress nested arrays or objects. (default: true)
- depth (int|null) Maximum depth for recursion.
null
means no limit. - throwable (bool) If
true
, throwsInvalidArgumentException
for invalid conditions. (default: true)
- clone (bool) If
- $currentDepth : int = 0
-
(Internal) Tracks current recursion depth. You usually don’t need to set this.
Tags
Return values
array<string|int, mixed> —The compressed array (or its clone if clone=true
).
deepMerge()
Recursively merges multiple arrays.
deepMerge(array<string|int, mixed> ...$arrays) : array<string|int, mixed>
- Associative keys (strings) are merged deeply : sub-arrays are merged récursivement.
- Numeric keys (ints) are appended, maintaining order.
Parameters
- $arrays : array<string|int, mixed>
-
The arrays to be merged.
Tags
Return values
array<string|int, mixed> —The deeply merged array.
delete()
Unset a key or nested key in an array using dot notation.
delete(mixed $target, string|array<string|int, mixed> $key[, string $separator = '.' ]) : mixed
- If the key is '*', clears the whole array.
- Supports string path ("a.b.c") or array path (['a','b','c']).
- If the path is partially invalid, the array is returned unchanged.
Parameters
- $target : mixed
-
The array to modify (ignored if not an array).
- $key : string|array<string|int, mixed>
-
The key path to delete.
- $separator : string = '.'
-
The separator for string key paths (default: '.').
Tags
Return values
mixed —The modified array (or original if not an array).
ensureArrayPath()
Ensures that a given key in an array contains a sub-array, and returns it by reference.
& ensureArrayPath(array<string|int, mixed> &$current, string $segment) : array<string|int, mixed>
If the key does not exist or is not an array, it initializes it as an empty array. This is useful for building nested structures dynamically using references.
Parameters
- $current : array<string|int, mixed>
-
The parent array passed by reference.
- $segment : string
-
The key to check or initialize as an array.
Tags
Return values
array<string|int, mixed> —A reference to the array stored at the specified key.
exists()
Checks whether a key exists in an array or an object implementing ArrayAccess.
exists(array<string|int, mixed>|ArrayAccess $array, string|int|null $key[, string $separator = '.' ]) : bool
- Returns
false
if the key isnull
or an empty string. - Supports both native arrays and
ArrayAccess
objects.
Parameters
- $array : array<string|int, mixed>|ArrayAccess
-
The array or object to inspect.
- $key : string|int|null
-
The key to check for existence.
- $separator : string = '.'
-
The separator used in the key path. Default is '.'.
Tags
Return values
bool —True if the key exists, false otherwise.
flatten()
Flattens a nested array into a single-level array.
flatten(array<string|int, mixed> $array) : array<string|int, mixed>
This function recursively iterates through the nested arrays, concatenating all nested values into one flat array.
Parameters
- $array : array<string|int, mixed>
-
The array to flatten.
Tags
Return values
array<string|int, mixed> —The flattened array, containing all nested values in order.
get()
Retrieves a value from an associative array using a key path.
get(array<string|int, mixed> $array, string|null $key[, mixed $default = null ][, string $separator = '.' ]) : mixed
This function allows navigation through a nested array using a key composed of segments separated by a specified separator (default is a dot). If the key is not found, the function returns a default value.
Parameters
- $array : array<string|int, mixed>
-
The associative array to search within.
- $key : string|null
-
The key path as a string. Can be null, in which case the function returns the entire array.
- $default : mixed = null
-
The default value to return if the key is not found. Defaults to null.
- $separator : string = '.'
-
The separator used to split the key into segments. Defaults to a dot ('.').
Tags
Return values
mixed —The value found in the array or the default value if the key does not exist.
getFirstKey()
Returns the first key from the provided list of keys that exists in the given array.
getFirstKey(array<string|int, mixed> $array, array<string|int, mixed> $keys[, mixed $default = null ]) : mixed
If none of the keys exist, the default value is returned.
Parameters
- $array : array<string|int, mixed>
-
The array to search within.
- $keys : array<string|int, mixed>
-
An ordered list of keys to check in the array.
- $default : mixed = null
-
The value to return if none of the keys exist in the array. Default is null.
Tags
Return values
mixed —Returns the first matching key if found, otherwise returns the default value.
getFirstValue()
Returns the value of the first key from the provided list of keys that exists in the given array.
getFirstValue(array<string|int, mixed> $array, array<string|int, mixed> $keys[, mixed $default = null ]) : mixed
If none of the keys exist, the default value is returned.
Parameters
- $array : array<string|int, mixed>
-
The array to search within.
- $keys : array<string|int, mixed>
-
An ordered list of keys to check in the array.
- $default : mixed = null
-
The value to return if none of the keys exist in the array. Default is null.
Tags
Return values
mixed —Returns the value corresponding to the first matching key if found, otherwise returns the default value.
hasIntKeys()
Indicates if all the keys in an array are integers.
hasIntKeys(array<string|int, mixed> $array) : bool
This function checks if every key in the provided array is an integer. Returns true only if all keys are integers, false otherwise.
Parameters
- $array : array<string|int, mixed>
-
The array to check.
Tags
Return values
bool —True if all keys are integers, false otherwise.
hasStringKeys()
Indicates if all the keys in an array are strings.
hasStringKeys(array<string|int, mixed> $array) : bool
This function checks if every key in the provided array is a string. Returns true only if all keys are strings, false otherwise.
Parameters
- $array : array<string|int, mixed>
-
The array to check.
Tags
Return values
bool —True if all keys are strings, false otherwise.
inBetween()
Insert an element between all the items in an array.
inBetween(array<string|int, mixed> $source, mixed $element) : array<string|int, mixed>
Parameters
- $source : array<string|int, mixed>
-
The source array
- $element : mixed
-
The element to insert between items
Tags
Return values
array<string|int, mixed> —A new array with elements inserted between original items
isAssociative()
Determines if an array is associative.
isAssociative(array<string|int, mixed> $array) : bool
Usage :
```php use function core\arrays\isAssociative ; $array = [ 'id' => 1 , 'created' => null , 'name' => 'hello world' , 'description' => null ]; echo json_encode( isAssociative( $array ) ) ; ```
Parameters
- $array : array<string|int, mixed>
-
The array to evaluate.
Tags
Return values
bool —Indicates if the array is associative.
isIndexed()
Determines if an array is indexed (i.e., has sequential integer keys starting at 0).
isIndexed(array<string|int, mixed> $array) : bool
An array is considered indexed if its keys are exactly the sequence 0, 1, 2, ... n-1, where n is the number of elements in the array. This is typical for arrays used as lists.
Empty arrays are considered indexed by definition.
Parameters
- $array : array<string|int, mixed>
-
The array to evaluate.
Tags
Return values
bool —True if the array is indexed, false otherwise.
removeKeys()
Removes a set of keys from an array.
removeKeys(array<string|int, mixed> $array[, array<string|int, mixed> $keys = [] ][, bool $clone = false ]) : array<string|int, mixed>
This function removes the specified keys from the given array.
If $clone
is set to true
, the original array is not modified; instead, a new array is returned.
Otherwise, the original array is modified directly (passed by value).
Parameters
- $array : array<string|int, mixed>
-
The input array to modify or clone.
- $keys : array<string|int, mixed> = []
-
An array of keys to remove from the input array.
- $clone : bool = false
-
If true, operates on a copy of the array and returns it; otherwise, modifies the array in place (by value).
Tags
Return values
array<string|int, mixed> —The resulting array with specified keys removed.
set()
Sets a value in an associative array using a key path.
set(array<string|int, mixed> &$array, string|null $key, mixed $value[, string $separator = '.' ][, bool $copy = false ]) : array<string|int, mixed>
If no key is given to the method, the entire array will be replaced.
Parameters
- $array : array<string|int, mixed>
-
The associative array to modify (by reference).
- $key : string|null
-
The key path as a string. Can be null, in which case the function replaces the entire array.
- $value : mixed
-
The value to set.
- $separator : string = '.'
-
The separator used to split the key into segments. Defaults to a dot ('.').
- $copy : bool = false
-
If true, returns a modified copy instead of altering the original array.
Tags
Return values
array<string|int, mixed> —The updated ( or copied and modified ) array.
setArrayValue()
Sets a value in a flat associative array using a single key.
setArrayValue(array<string|int, mixed> $document, string $key, mixed $value) : array<string|int, mixed>
This helper function assigns the given value to the specified key in the provided array. It does not support nested keys or separators.
The array is returned with the updated value, leaving the original array unmodified if passed by value.
Parameters
- $document : array<string|int, mixed>
-
The associative array to modify.
- $key : string
-
The key at which to set the value.
- $value : mixed
-
The value to assign to the key.
Tags
Return values
array<string|int, mixed> —The modified array with the new or updated key/value pair.
shuffle()
Shuffles the elements of an array in place using the Fisher-Yates algorithm.
shuffle(array<string|int, mixed> &$ar) : array<string|int, mixed>
This function randomizes the order of the elements in the given array. It operates directly on the passed array (by reference) and also returns it. The algorithm used ensures an unbiased shuffle.
If the array has 0 or 1 element, it is returned as is without changes.
Parameters
- $ar : array<string|int, mixed>
-
The array to shuffle (passed by reference).
Tags
Return values
array<string|int, mixed> —The shuffled array.
stub()
Returns a new empty array.
stub() : array<string|int, mixed>
This function is useful as a placeholder or default value generator when an empty array is needed.
Tags
Return values
array<string|int, mixed> —An empty array.
swap()
Swaps two indexed values in a specific array representation.
swap(array<string|int, mixed> $ar[, int|string $from = 0 ][, int|string $to = 0 ][, bool $copy = false ]) : array<string|int, mixed>
Supports both integer and string keys. Negative indices are supported to count from the end (e.g. -1 is the last element).
If $copy
is true, returns a new array with swapped values, leaving original intact.
Otherwise, swaps values directly on the passed array.
Parameters
- $ar : array<string|int, mixed>
-
The array of values to change
- $from : int|string = 0
-
The first key/index position to swap (default: 0)
- $to : int|string = 0
-
The second key/index position to swap (default: 0)
- $copy : bool = false
-
If true, returns a swapped clone of the passed-in array (default: false)
Tags
Return values
array<string|int, mixed> —The modified array reference.
tail()
Returns a new array containing all elements except the first one.
tail(array<string|int, mixed> $array) : array<string|int, mixed>
If the input array is empty, returns an empty array.
Parameters
- $array : array<string|int, mixed>
-
The input array.
Tags
Return values
array<string|int, mixed> —A new array without the first element.
toArray()
Ensures the given value is returned as an array.
toArray(mixed $value) : array<string|int, mixed>
If the value is already an array, it is returned unchanged. Otherwise, the value is wrapped inside a new array.
Parameters
- $value : mixed
-
The value to encapsulate in an array.
Tags
Return values
array<string|int, mixed> —The value wrapped in an array, or the original array if already an array.
unique()
Removes duplicate values from an array and reindexes it.
unique(array<string|int, mixed> $array[, int $flags = SORT_STRING ]) : array<string|int, mixed>
Combines the functionality of array_unique() and array_values(): first removes duplicates, then reindexes the array numerically starting at 0.
The optional $flags
parameter modifies the comparison behavior, possible values:
- SORT_REGULAR: Compare items normally (don't change types).
- SORT_NUMERIC: Compare items numerically.
- SORT_STRING: Compare items as strings.
- SORT_LOCALE_STRING: Compare items as strings, based on the current locale.
Parameters
- $array : array<string|int, mixed>
-
The input array.
- $flags : int = SORT_STRING
-
[optional] Comparison behavior flag. Default is SORT_STRING.
Tags
Return values
array<string|int, mixed> —The filtered array with unique values, reindexed from 0.