Skip to content

Drupal\helper\ArrayHelper

Provides helper for working with arrays.

Methods

Name Description
addUniqueValue Add a value to an array if it is not already present.
chunkEvenly Split an array into chunks more evenly then array_chunk().
filterKeys Filters keys of an array using a callback function.
hasValue Checks if an array contains a value.
mapKeys Applies the callback to the keys of the given array.
removeValue Remove an array's value.
replaceKey Replace array key.
replaceKeys Replace array keys.
replaceValue Replace array value.
replaceValues Replace multiple array values.

ArrayHelper::addUniqueValue

Description

public static addUniqueValue (array $array, mixed $value, mixed|null $key, bool $strict)

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

Parameters

  • (array) $array : The array, modified by reference.
  • (mixed) $value : The value to add.
  • (mixed|null) $key : The key to use for the value if it is needed.
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

bool

TRUE if the value was added, or FALSE otherwise.


ArrayHelper::chunkEvenly

Description

public static chunkEvenly (array $array, int $chunks, bool $preserve_keys)

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

Parameters

  • (array) $array : An array of data to split up.
  • (int) $chunks : The amount of chunks to create.
  • (bool) $preserve_keys : When set to TRUE keys will be preserved. Default is FALSE which will
    reindex the chunk numerically.

Return Values

array

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

Throws Exceptions

\DomainException


ArrayHelper::filterKeys

Description

public static filterKeys (array $array, callable|null $callback)

Filters keys of an array using a callback function.

Parameters

  • (array) $array : The array to iterate over.
  • (callable|null) $callback : The callback function to use. If no callback is supplied, all keys of
    array equal to FALSE will be removed.

Return Values

array

Returns the filtered array.


ArrayHelper::hasValue

Description

public static hasValue (array $array, mixed $value, bool $strict, array|null $keys)

Checks if an array contains a value.

Parameters

  • (array) $array : The array.
  • (mixed) $value : The value to search for.
  • (bool) $strict : Passed to array_search() for strict comparison.
  • (array|null) $keys : The keys of the values if found, set by reference.

Return Values

bool

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


ArrayHelper::mapKeys

Description

public static mapKeys (array $array, callable $callback)

Applies the callback to the keys of the given array.

Parameters

  • (array) $array : An array to run through the callback function.
  • (callable) $callback : Callback function to run for each key in the array.

Return Values

array

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


ArrayHelper::removeValue

Description

public static removeValue (array $array, mixed $value, bool $strict)

Remove an array's value.

Parameters

  • (array) $array : The array, modified by reference.
  • (mixed) $value : The value to search for.
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

bool

TRUE if the item was removed, or FALSE otherwise.


ArrayHelper::replaceKey

Description

public static replaceKey (array $array, mixed $search, mixed $replace, bool $strict)

Replace array key.

Parameters

  • (array) $array : The array, modified by reference.
  • (mixed) $search : The key to search for.
  • (mixed) $replace : The replacement key.
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

void


ArrayHelper::replaceKeys

Description

public static replaceKeys (array $array, array $replacements, bool $strict)

Replace array keys.

Parameters

  • (array) $array : The array, modified by reference.
  • (array) $replacements : The replacement keys, keyed by their original values.
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

void


ArrayHelper::replaceValue

Description

public static replaceValue (array $array, mixed $search, mixed $replace, bool $strict)

Replace array value.

Parameters

  • (array) $array : The array.
  • (mixed) $search : The value(s) to search for.
  • (mixed) $replace : The replacement value(s).
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

void


ArrayHelper::replaceValues

Description

public static replaceValues (array $array, array $replacements, bool $strict)

Replace multiple array values.

Parameters

  • (array) $array : The array.
  • (array) $replacements : The replacement values, keyed by their original values.
  • (bool) $strict : Passed to array_search() for strict comparison.

Return Values

void