Skip to content

ArrayHelper

Provides helper for working with arrays.


  • Full name: \Drupal\helper\ArrayHelper

Methods

hasValue

Checks if an array contains a value.

public static hasValue(array $array, mixed $value, bool $strict = true, array|null& $keys = null): bool
  • This method is static. Parameters:
Parameter Type Description
$array array
The array.
$value mixed
The value to search for.
$strict bool
Passed to array_search() for strict comparison.
$keys array|null
The keys of the values if found, set by reference.

Return Value:

TRUE if the array has the value, or FALSE otherwise.


replaceValue

Replace array value.

public static replaceValue(array& $array, mixed $search, mixed $replace, bool $strict = true): void

Every value that matches is replaced, and the keys of the array are kept. Unlike ::replaceValues(), the search can be any type, such as TRUE, NULL or an array. With strict comparison, a value only matches if it has the same type, so a '1' search does not replace the integer 1, while ::replaceValues() with a '1' replacement replaces both.

  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$search mixed
The value to search for.
$replace mixed
The replacement value.
$strict bool
Whether to compare values with === instead of ==. Passed to
array_keys().

replaceValues

Replace multiple array values at once.

public static replaceValues(array& $array, array $replacements, bool $strict = true): void

Each value is replaced at most once, based on the original values, like strtr() does for strings. For example, ['a' => 'b', 'b' => 'c'] replaces 'a' with 'b' and 'b' with 'c', and ['a' => 'b', 'b' => 'a'] swaps them. The keys of the array are kept.

To replace values by key instead, use array_replace().

  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$replacements array
The replacement values, keyed by their original values.
$strict bool
Whether values must match a replacement key exactly. Integer and string
values always match the way PHP matches array keys, so the integer 1 and
the string '1' both match a '1' replacement. Without strict comparison,
any other value is compared to each replacement key with ==, in order,
so '01', 1.0 and TRUE also match a '1' replacement. Since keys are
always integers or strings, values of other types, such as TRUE or NULL,
are only replaced without strict comparison.

replaceKey

Replace an array key.

public static replaceKey(array& $array, int|string $search, int|string $replace): void

The order of the array is kept. If the replacement key already exists, only the value of the later of the two keys is kept, in the position of the earlier key. For example, replacing 'x' with 'y' in ['x' => 1, 'y' => 2] results in ['y' => 2], and in ['y' => 2, 'x' => 1] results in ['y' => 1].

  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$search int|string
The key to search for. Numeric string keys are matched the same way PHP
stores them, so '1' finds the integer key 1.
$replace int|string
The replacement key.

replaceKeys

Replace multiple array keys at once.

public static replaceKeys(array& $array, array<int|string,int|string> $replacements): void

Each key is replaced at most once, based on the original keys, like strtr() does for strings. For example, ['x' => 'y', 'y' => 'z'] on ['x' => 1, 'y' => 2] results in ['y' => 1, 'z' => 2], and ['x' => 'y', 'y' => 'x'] swaps the keys. The order of the array is kept.

If two keys end up the same, only the value of the later key is kept, in the position of the earlier key. For example, ['x' => 'y'] on ['x' => 1, 'y' => 2] results in ['y' => 2].

  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$replacements array
The replacement keys, keyed by their original keys. PHP stores numeric
string keys as integers, so a '1' replacement finds the integer key 1.

addUniqueValue

Add a value to an array if it is not already present.

public static addUniqueValue(array& $array, mixed $value, int|string|null $key = null, bool $strict = true): bool
  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$value mixed
The value to add.
$key int|string|null
The key to use for the value if it is needed.
$strict bool
Passed to array_search() for strict comparison.

Return Value:

TRUE if the value was added, or FALSE otherwise.


removeValue

Remove an array's value.

public static removeValue(array& $array, mixed $value, bool $strict = false): bool
  • This method is static. Parameters:
Parameter Type Description
$array array
The array, modified by reference.
$value mixed
The value to search for.
$strict bool
Passed to array_search() for strict comparison.

Return Value:

TRUE if the item was removed, or FALSE otherwise.


mapKeys

Applies the callback to the keys of the given array.

public static mapKeys(array $array, callable $callback): array
  • This method is static. Parameters:
Parameter Type Description
$array array
An array to run through the callback function.
$callback callable
Callback function to run for each key in the array.

Return Value:

Returns an array containing all the elements of $array after applying the callback function to each key.


filterKeys

Filters keys of an array using a callback function.

public static filterKeys(array $array, callable|null $callback = null): array
  • This method is static. Parameters:
Parameter Type Description
$array array
The array to iterate over.
$callback callable|null
The callback function to use. If no callback is supplied, all keys of
array equal to FALSE will be removed.

Return Value:

Returns the filtered array.


chunkEvenly

Split an array into chunks more evenly then array_chunk().

public static chunkEvenly(array $array, int $chunks, bool $preserve_keys = false): array
  • This method is static. Parameters:
Parameter Type Description
$array array
An array of data to split up.
$chunks int
The amount of chunks to create.
$preserve_keys bool
When set to TRUE keys will be preserved. Default is FALSE which will
reindex the chunk numerically.

Return Value:

Returns a multidimensional numerically indexed array, starting with zero, with each dimension containing size elements.

Throws:

  • DomainException

See Also:

  • http://php.net/manual/en/function.array-chunk.php#75022

spliceAssoc

Removes part of an associative array and replaces it, keeping the keys.

public static spliceAssoc(array& $array, int $offset, int|null $length = null, array $values = []): void

This works like array_splice(), except that the keys of the array and of the inserted values are kept, including integer keys. Inserted values replace any existing values with the same keys, and take their new position. To start at a key instead of a position, use ::spliceAtKey().

Because integer keys are kept, an inserted value with the same integer key as an existing value replaces it. To insert into a list, use array_splice() instead.

  • This method is static. Parameters:
Parameter Type Description
$array array
The associative array.
$offset int
The position to start removing at. A negative position counts from the
end of the array.
$length int|null
The number of elements to remove. A negative number keeps that many
elements at the end of the array. NULL removes everything from the
offset to the end of the array.
$values array
The associative array of values to insert at the offset.

See Also:

  • \Drupal\helper\array_splice()

spliceAtKey

Removes part of an associative array starting at a key, and replaces it.

public static spliceAtKey(array& $array, int|string $key, int|null $length = null, array $values = []): void

This works like ::spliceAssoc(), but starts at the element with a key rather than at a position, so it works with any keys, including integer keys that are not sequential.

  • This method is static. Parameters:
Parameter Type Description
$array array
The associative array.
$key int|string
The key of the first element to remove, or to insert the values before.
$length int|null
The number of elements to remove. A negative number keeps that many
elements at the end of the array. NULL removes everything from the key
to the end of the array.
$values array
The associative array of values to insert where the key was.

Throws:

Thrown when the key is not in the array. - InvalidArgumentException


getKeyPosition

Gets the position of a key in an array.

public static getKeyPosition(array $array, int|string $key): int|null

Numeric string keys are matched the same way PHP stores them, so '1' finds the integer key 1.

  • This method is static. Parameters:
Parameter Type Description
$array array
The array.
$key int|string
The key.

Return Value:

The position of the key, starting at 0, or NULL if the key is not in the array.


insertBeforeKey

Inserts values before a key in an associative array.

public static insertBeforeKey(array& $array, int|string $key, array $values): bool

If the key is not found, the values are inserted at the beginning of the array. See ::spliceAssoc() for how existing keys are handled.

  • This method is static. Parameters:
Parameter Type Description
$array array
The associative array.
$key int|string
The key to insert the values before.
$values array
The associative array of values to insert.

Return Value:

TRUE if the key was found, or FALSE if the values were inserted at the beginning of the array instead.


insertAfterKey

Inserts values after a key in an associative array.

public static insertAfterKey(array& $array, int|string $key, array $values): bool

If the key is not found, the values are added to the end of the array. See ::spliceAssoc() for how existing keys are handled.

  • This method is static. Parameters:
Parameter Type Description
$array array
The associative array.
$key int|string
The key to insert the values after.
$values array
The associative array of values to insert.

Return Value:

TRUE if the key was found, or FALSE if the values were added to the end of the array instead.