ArrayHelper

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\ArrayHelper » yii\helpers\ArrayHelper (opens new window) » yii\helpers\BaseArrayHelper (opens new window)
Since
3.0.0

Class ArrayHelper

View source (opens new window)

# Public Methods

Method Description
append() Appends values to an array.
contains() Returns whether the given array contains any values where a given key (the name of a sub-array key or sub-object property) is set to a given value.
ensureNonAssociative() Ensures an array is non-associative.
filter() (opens new window) Filters array according to rules specified.
filterByValue() Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.
filterEmptyStringsFromArray() Filters empty strings from an array.
firstKey() Returns the first key in a given array.
firstValue() Returns the first value in a given array.
firstWhere() Returns the first value in a given array where a given key (the name of a sub-array key or sub-object property) is set to a given value.
getColumn() (opens new window) Returns the values of a specified column in an array.
getValue() (opens new window) Retrieves the value of an array element or object property with the given key or property name.
htmlDecode() (opens new window) Decodes HTML entities into the corresponding characters in an array of strings.
htmlEncode() (opens new window) Encodes special characters in an array of strings into HTML entities.
index() (opens new window) Indexes and/or groups the array according to a specified key.
isAssociative() (opens new window) Returns a value indicating whether the given array is an associative array.
isIn() (opens new window) Check whether an array or Traversable (opens new window) contains an element.
isIndexed() (opens new window) Returns a value indicating whether the given array is an indexed array.
isNumeric() Returns whether all the elements in the array are numeric.
isOrdered() Checks whether a numerically-indexed array's keys are in ascending order.
isSubset() (opens new window) Checks whether an array or Traversable (opens new window) is a subset of another array or Traversable (opens new window).
isTraversable() (opens new window) Checks whether a variable is an array or Traversable (opens new window).
keyExists() (opens new window) Checks if the given array contains the specified key.
map() (opens new window) Builds a map (key-value pairs) from a multidimensional array or an array of objects.
merge() (opens new window) Merges two or more arrays into one recursively.
multisort() (opens new window) Sorts an array of objects or arrays (with the same structure) by one or several keys.
prepend() Prepends values to an array.
prependOrAppend() Prepends or appends a value to an array.
remove() (opens new window) Removes an item from an array and returns the value. If the key does not exist in the array, the default value will be returned instead.
removeValue() (opens new window) Removes items with matching values from the array and returns the removed items.
rename() Renames an item in an array. If the new key already exists in the array and the old key doesn’t, the array will be left unchanged.
setValue() (opens new window) Writes a value into an associative array at the key path specified.
toArray() Converts an object or an array of objects into an array.
where() Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.
whereIn() Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to one of a given range of values.
whereMultiple() Filters an array to only the values where a list of keys is set to given values.
without() Returns a copy of an array without a given key.
withoutValue() Returns a copy of an array without items with matching the given value.

# append()

Since
3.4.0

Appends values to an array.

This should be used instead of array_push($array, ...$values) when $values could be an empty array, as PHP < 7.3 would throw an error in that case.

View source (opens new window)

Arguments

  • $array
  • $values (mixed) – The values to append.

Example

ArrayHelper::append($array, ...$values);

# contains()

Since
3.4.0

Returns whether the given array contains any values where a given key (the name of a sub-array key or sub-object property) is set to a given value.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the value exists in the array

# ensureNonAssociative()

Since
3.1.17.1

Ensures an array is non-associative.

View source (opens new window)

Arguments

# filterByValue()

DEPRECATED

Deprecated in 3.2.0. Use where() instead.

Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.

Array keys are preserved.

View source (opens new window)

Arguments

Returns

array (opens new window) – The filtered array

# filterEmptyStringsFromArray()

Filters empty strings from an array.

View source (opens new window)

Arguments

Returns

array (opens new window)

# firstKey()

Returns the first key in a given array.

View source (opens new window)

Arguments

Returns

string (opens new window), integer (opens new window), null (opens new window) – The first key, whether that is a number (if the array is numerically indexed) or a string, or null if $arr isn’t an array, or is empty.

# firstValue()

Returns the first value in a given array.

View source (opens new window)

Arguments

Returns

mixed – The first value, or null if $arr isn’t an array, or is empty.

# firstWhere()

Since
3.1.0

Returns the first value in a given array where a given key (the name of a sub-array key or sub-object property) is set to a given value.

View source (opens new window)

Arguments

Returns

mixed – The value, or null if it can't be found

# isNumeric()

Since
3.5.0

Returns whether all the elements in the array are numeric.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# isOrdered()

Since
3.4.0

Checks whether a numerically-indexed array's keys are in ascending order.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# prepend()

Since
3.4.0

Prepends values to an array.

This should be used instead of array_unshift($array, ...$values) when $values could be an empty array, as PHP < 7.3 would throw an error in that case.

View source (opens new window)

Arguments

  • $array
  • $values (mixed) – The values to prepend.

Example

ArrayHelper::prepend($array, ...$values);

# prependOrAppend()

Prepends or appends a value to an array.

View source (opens new window)

Arguments

  • $array
  • $value (mixed) – The value to prepend/append to the array
  • $prepend (boolean (opens new window)) – true will prepend the value; false will append it

# rename()

Renames an item in an array. If the new key already exists in the array and the old key doesn’t, the array will be left unchanged.

View source (opens new window)

Arguments

# toArray()

Converts an object or an array of objects into an array.

View source (opens new window)

Arguments

[
    'app\models\Post' => [
        'id',
        'title',
        // the key name in array result => property name
        'createTime' => 'created_at',
        // the key name in array result => anonymous function
        'length' => function ($post) {
            return strlen($post->content);
        },
    ],
]

The result of ArrayHelper::toArray($post, $properties) could be like the following:

[
    'id' => 123,
    'title' => 'test',
    'createTime' => '2013-01-01 12:00AM',
    'length' => 301,
]

Returns

array (opens new window) – The array representation of the object

# where()

Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to a given value.

Array keys are preserved by default.

View source (opens new window)

Arguments

Returns

array (opens new window) – The filtered array

# whereIn()

Since
3.5.8

Filters an array to only the values where a given key (the name of a sub-array key or sub-object property) is set to one of a given range of values.

Array keys are preserved by default.

View source (opens new window)

Arguments

Returns

array (opens new window) – The filtered array

# whereMultiple()

Since
3.3.0

Filters an array to only the values where a list of keys is set to given values.

Array keys are preserved.

This method is most useful when, given an array of elements, it is needed to filter them by multiple conditions.

Below are some usage examples,

// Entries with certain entry types
$filtered = \craft\helpers\ArrayHelper::whereMultiple($entries, ['typeId' => [2, 4]]);

// Entries with multiple conditions
$filtered = \craft\helpers\ArrayHelper::whereMultiple($entries, ['typeId' => 2, 'authorId' => [1, 2]);

// Testing for an array value
$filtered = \craft\helpers\ArrayHelper::whereMultiple($asset, ['focalPoint' => [['x' => 0.5, 'y' => 0.5]]]);

View source (opens new window)

Arguments

Returns

array (opens new window) – The filtered array

# without()

Since
3.0.9

Returns a copy of an array without a given key.

View source (opens new window)

Arguments

Returns

array (opens new window)

# withoutValue()

Returns a copy of an array without items with matching the given value.

View source (opens new window)

Arguments

Returns

array (opens new window)