DataStructureArray
Array Data Structure helper
Provides utility methods to assist in manipulating arrays.
Attributes
- #[AllowDynamicProperties]
Table of Contents
Methods
| flatten() | Flattens a multi-dimensional array into a single level array, using the provided notation. |
| keyToNumeric() | Converts a key/value paired array to a numerically indexed array that contains key/value pairs. |
| numericToKey() | Converts a numerically indexed array to a key/value indexed array. |
| unflatten() | Unflattens a single level array into a multi-dimensional array, using the provided notation. |
Methods
flatten()
Flattens a multi-dimensional array into a single level array, using the provided notation.
public
static flatten(array<string|int, mixed> $array[, string $prefix = '[' ][, string $suffix = ']' ][, bool $cast_object = false ][, string $parent_key = '' ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
A key/value array to convert to a single level indexed array
- $prefix : string = '['
-
The prefix to be used in the notation
- $suffix : string = ']'
-
The suffix to be used in the notation
- $cast_object : bool = false
-
True to cast objects as arrays (optional, default false)
- $parent_key : string = ''
-
The parent key from the current array (used for recursion)
Return values
array<string|int, mixed> —A single level array, flattened using the provided notation
keyToNumeric()
Converts a key/value paired array to a numerically indexed array that contains key/value pairs.
public
static keyToNumeric(mixed $vars[, bool $match_indexes = true ]) : mixed
Parameters
- $vars : mixed
-
A key/value array or object to convert to a numerically indexed array
- $match_indexes : bool = true
-
True will ensure that each key index contains the same number of elements. The default value for each index is null.
numericToKey()
Converts a numerically indexed array to a key/value indexed array.
public
static numericToKey(array<string|int, mixed> $array[, string $key_index = null ][, string $value_index = null ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
A numerically indexed array of key/value pairs (array or object)
- $key_index : string = null
-
An index to convert from key/value if set, otherwise will invert the array
- $value_index : string = null
-
The index from $array to use as the value in the key/value pair. By default will assign everything in $array[$i] to the key.
Return values
array<string|int, mixed> —A key/value paired array
unflatten()
Unflattens a single level array into a multi-dimensional array, using the provided notation.
public
static unflatten(array<string|int, mixed> $array[, string $prefix = '[' ][, string $suffix = ']' ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
A key/value array to convert to a multi-dimensional indexed array
- $prefix : string = '['
-
The prefix used in the notation of the array
- $suffix : string = ']'
-
The suffix used in the notation of the array
Return values
array<string|int, mixed> —A single level array, flattened using the provided notation