# BaseArrayHelper

> BaseArrayHelper provides concrete implementation for yii\helpers\ArrayHelper.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [helpers](/docs/api/yii/helpers/index.md) › BaseArrayHelper

| class | `yii\helpers\BaseArrayHelper` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/helpers/BaseArrayHelper.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/helpers/BaseArrayHelper.php#L25) |

1 subclass:

- [`ArrayHelper`](/docs/api/yii/helpers/arrayhelper/index.md)

BaseArrayHelper provides concrete implementation for [`ArrayHelper`](/docs/api/yii/helpers/arrayhelper/index.md).

Do not use BaseArrayHelper. Use [`ArrayHelper`](/docs/api/yii/helpers/arrayhelper/index.md) instead.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`filter()`](#method-filter) static | Filters array according to rules specified. | `BaseArrayHelper` |
| [`flatten()`](#method-flatten) static | Flattens a multidimensional array into a one-dimensional array. | `BaseArrayHelper` |
| [`getColumn()`](#method-getColumn) static | Returns the values of a specified column in an array. | `BaseArrayHelper` |
| [`getValue()`](#method-getValue) static | Retrieves the value of an array element or object property with the given key or property name. | `BaseArrayHelper` |
| [`htmlDecode()`](#method-htmlDecode) static | Decodes HTML entities into the corresponding characters in an array of strings. | `BaseArrayHelper` |
| [`htmlEncode()`](#method-htmlEncode) static | Encodes special characters in an array of strings into HTML entities. | `BaseArrayHelper` |
| [`index()`](#method-index) static | Indexes and/or groups the array according to a specified key. | `BaseArrayHelper` |
| [`isAssociative()`](#method-isAssociative) static | Returns a value indicating whether the given array is an associative array. | `BaseArrayHelper` |
| [`isIn()`](#method-isIn) static | Check whether an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php) contains an element. | `BaseArrayHelper` |
| [`isIndexed()`](#method-isIndexed) static | Returns a value indicating whether the given array is an indexed array. | `BaseArrayHelper` |
| [`isSubset()`](#method-isSubset) static | Checks whether an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php) is a subset of another array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php). | `BaseArrayHelper` |
| [`isTraversable()`](#method-isTraversable) static | Checks whether a variable is an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php). | `BaseArrayHelper` |
| [`keyExists()`](#method-keyExists) static | Checks if the given array contains the specified key. | `BaseArrayHelper` |
| [`map()`](#method-map) static | Builds a map (key-value pairs) from a multidimensional array or an array of objects. | `BaseArrayHelper` |
| [`merge()`](#method-merge) static | Merges two or more arrays into one recursively. | `BaseArrayHelper` |
| [`multisort()`](#method-multisort) static | Sorts an array of objects or arrays (with the same structure) by one or several keys. | `BaseArrayHelper` |
| [`recursiveSort()`](#method-recursiveSort) static | Sorts array recursively. | `BaseArrayHelper` |
| [`remove()`](#method-remove) static | 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. | `BaseArrayHelper` |
| [`removeValue()`](#method-removeValue) static | Removes items with matching values from the array and returns the removed items. | `BaseArrayHelper` |
| [`setValue()`](#method-setValue) static | Writes a value into an associative array at the key path specified. | `BaseArrayHelper` |
| [`toArray()`](#method-toArray) static | Converts an object or an array of objects into an array. | `BaseArrayHelper` |

## Method details

### filter() <span id="method-filter"></span>

```php
public static function filter(array $array, iterable $filters): array
```

Filters array according to rules specified.

For example:

```
$array = [
    'A' => [1, 2],
    'B' => [
        'C' => 1,
        'D' => 2,
    ],
    'E' => 1,
];

$result = \yii\helpers\ArrayHelper::filter($array, ['A']);
// $result will be:
// [
//     'A' => [1, 2],
// ]

$result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']);
// $result will be:
// [
//     'A' => [1, 2],
//     'B' => ['C' => 1],
// ]

$result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']);
// $result will be:
// [
//     'B' => ['D' => 2],
// ]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | Source array |
| `$filters` | `iterable` | Rules that define array keys which should be left or removed from results. Each rule is: - `var` - `$array['var']` will be left in result. - `var.key` = only `$array['var']['key'] will be left in result. - `!var.key` = `$array['var']['key'] will be removed from result. |

**Returns**

`array`: Filtered array

**Since**

2.0.9

### flatten() <span id="method-flatten"></span>

```php
public static function flatten(array $array, string $separator = '.'): array
```

Flattens a multidimensional array into a one-dimensional array.

This method recursively traverses the input array and concatenates the keys
in a dot format to form a new key in the resulting array.

Example:

```
$array = [
     'A' => [1, 2],
     'B' => [
         'C' => 1,
         'D' => 2,
     ],
     'E' => 1,
 ];
$result = \yii\helpers\ArrayHelper::flatten($array);
// $result will be:
// [
//     'A.0' => 1
//     'A.1' => 2
//     'B.C' => 1
//     'B.D' => 2
//     'E' => 1
// ]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The input array to be flattened in terms of name-value pairs. |
| `$separator` | `string` | The separator to use between keys. Defaults to '.'. |

**Returns**

`array`: The flattened array.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if `$array` is neither traversable nor an array.

### getColumn() <span id="method-getColumn"></span>

```php
public static function getColumn(array $array, int|string|array|Closure $name, bool $keepKeys = true): array
```

Returns the values of a specified column in an array.

The input array should be multidimensional or an array of objects.

For example,

```
$array = [
    ['id' => '123', 'data' => 'abc'],
    ['id' => '345', 'data' => 'def'],
];
$result = ArrayHelper::getColumn($array, 'id');
// the result is: ['123', '345']

// using anonymous function
$result = ArrayHelper::getColumn($array, function ($element) {
    return $element['id'];
});
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` |  |
| `$name` | `int\|string\|array\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) |  |
| `$keepKeys` | `bool` | Whether to maintain the array keys. If false, the resulting array will be re-indexed with integers. |

**Returns**

`array`: The list of column values

### getValue() <span id="method-getValue"></span>

```php
public static function getValue(array|object $array, string|Closure|array $key, mixed $default = null): mixed
```

Retrieves the value of an array element or object property with the given key or property name.

If the key does not exist in the array, the default value will be returned instead.
Not used when getting value from an object.

The key may be specified in a dot format to retrieve the value of a sub-array or the property
of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
or `$array->x` is neither an array nor an object, the default value will be returned.
Note that if the array already has an element `x.y.z`, then its value will be returned
instead of going through the sub-arrays. So it is better to be done specifying an array of key names
like `['x', 'y', 'z']`.

Below are some usage examples,

```
// working with array
$username = \yii\helpers\ArrayHelper::getValue($_POST, 'username');
// working with object
$username = \yii\helpers\ArrayHelper::getValue($user, 'username');
// working with anonymous function
$fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) {
    return $user->firstName . ' ' . $user->lastName;
});
// using dot format to retrieve the property of embedded object
$street = \yii\helpers\ArrayHelper::getValue($users, 'address.street');
// using an array of keys to retrieve the value
$value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array\|object` | Array or object to extract value from |
| `$key` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|array` | Key name of the array element, an array of keys or property name of the object, or an anonymous function returning the value. The anonymous function signature should be: `function($array, $defaultValue)`. The possibility to pass an array of keys is available since version 2.0.4. |
| `$default` | `mixed` | The default value to be returned if the specified array key does not exist. Not used when getting value from an object. |

**Returns**

`mixed`: The value of the element if found, default value otherwise

### htmlDecode() <span id="method-htmlDecode"></span>

```php
public static function htmlDecode(array $data, bool $valuesOnly = true): array
```

Decodes HTML entities into the corresponding characters in an array of strings.

Only array values will be decoded by default.
If a value is an array, this method will also decode it recursively.
Only string values will be decoded.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | Data to be decoded |
| `$valuesOnly` | `bool` | Whether to decode array values only. If `false`, then both the array keys and array values will be decoded. |

**Returns**

`array`: The decoded data

**See also**

- <https://www.php.net/manual/en/function.htmlspecialchars-decode.php>

### htmlEncode() <span id="method-htmlEncode"></span>

```php
public static function htmlEncode(array $data, bool $valuesOnly = true, string|null $charset = null): array
```

Encodes special characters in an array of strings into HTML entities.

Only array values will be encoded by default.
If a value is an array, this method will also encode it recursively.
Only string values will be encoded.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | Data to be encoded |
| `$valuesOnly` | `bool` | Whether to encode array values only. If false, both the array keys and array values will be encoded. |
| `$charset` | `string\|null` | The charset that the data is using. If not set, [`Application::$charset`](/docs/api/yii/base/application/index.md#property-charset) will be used. |

**Returns**

`array`: The encoded data

**See also**

- <https://www.php.net/manual/en/function.htmlspecialchars.php>

### index() <span id="method-index"></span>

```php
public static function index(array $array, string|Closure|null $key, string|string[]|Closure[]|null $groups = []): array
```

Indexes and/or groups the array according to a specified key.

The input should be either multidimensional array or an array of objects.

The $key can be either a key name of the sub-array, a property name of object, or an anonymous
function that must return the value that will be used as a key.

$groups is an array of keys, that will be used to group the input array into one or more sub-arrays based
on keys specified.

If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition
to `$groups` not specified then the element is discarded.

For example:

```
$array = [
    ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
    ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
];
$result = ArrayHelper::index($array, 'id');
```

The result will be an associative array, where the key is the value of `id` attribute

```
[
    '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
    // The second element of an original array is overwritten by the last element because of the same id
]
```

An anonymous function can be used in the grouping array as well.

```
$result = ArrayHelper::index($array, function ($element) {
    return $element['id'];
});
```

Passing `id` as a third argument will group `$array` by `id`:

```
$result = ArrayHelper::index($array, null, 'id');
```

The result will be a multidimensional array grouped by `id`:

```
[
    '123' => [
        ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
    ],
    '345' => [ // all elements with this index are present in the result array
        ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
        ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
    ]
]
```

The anonymous function can be used in the array of grouping keys as well:

```
$result = ArrayHelper::index($array, 'data', [function ($element) {
    return $element['id'];
}, 'device']);
```

The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one
and indexed by the `data` on the third level:

```
[
    '123' => [
        'laptop' => [
            'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
        ]
    ],
    '345' => [
        'tablet' => [
            'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet']
        ],
        'smartphone' => [
            'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
        ]
    ]
]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array that needs to be indexed or grouped |
| `$key` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|null` | The column name or anonymous function which result will be used to index the array |
| `$groups` | `string\|string[]\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`[]\|null` | The array of keys, that will be used to group the input array by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added to the result array without any key. This parameter is available since version 2.0.8. |

**Returns**

`array`: The indexed and/or grouped array

### isAssociative() <span id="method-isAssociative"></span>

```php
public static function isAssociative(array $array, bool $allStrings = true): bool
```

Returns a value indicating whether the given array is an associative array.

An array is associative if all its keys are strings. If `$allStrings` is false,
then an array will be treated as associative if at least one of its keys is a string.

Note that an empty array will NOT be considered associative.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array being checked |
| `$allStrings` | `bool` | Whether the array keys must be all strings in order for the array to be treated as associative. |

**Returns**

`bool`: Whether the array is associative

### isIn() <span id="method-isIn"></span>

```php
public static function isIn(mixed $needle, iterable $haystack, bool $strict = false): bool
```

Check whether an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php) contains an element.

This method does the same as the PHP function [in_array()](https://www.php.net/manual/en/function.in-array.php)
but additionally works for objects that implement the [`Traversable`](https://www.php.net/manual/en/class.traversable.php) interface.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$needle` | `mixed` | The value to look for. |
| `$haystack` | `iterable` | The set of values to search. |
| `$strict` | `bool` | Whether to enable strict (`===`) comparison. |

**Returns**

`bool`: `true` if `$needle` was found in `$haystack`, `false` otherwise.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if `$haystack` is neither traversable nor an array.

**Since**

2.0.7

**See also**

- <https://www.php.net/manual/en/function.in-array.php>

### isIndexed() <span id="method-isIndexed"></span>

```php
public static function isIndexed(array $array, bool $consecutive = false): bool
```

Returns a value indicating whether the given array is an indexed array.

An array is indexed if all its keys are integers. If `$consecutive` is true,
then the array keys must be a consecutive sequence starting from 0.

Note that an empty array will be considered indexed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array being checked |
| `$consecutive` | `bool` | Whether the array keys must be a consecutive sequence in order for the array to be treated as indexed. |

**Returns**

`bool`: Whether the array is indexed

### isSubset() <span id="method-isSubset"></span>

```php
public static function isSubset(iterable $needles, iterable $haystack, bool $strict = false): bool
```

Checks whether an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php) is a subset of another array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php).

This method will return `true`, if all elements of `$needles` are contained in
`$haystack`. If at least one element is missing, `false` will be returned.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$needles` | `iterable` | The values that must **all** be in `$haystack`. |
| `$haystack` | `iterable` | The set of value to search. |
| `$strict` | `bool` | Whether to enable strict (`===`) comparison. |

**Returns**

`bool`: `true` if `$needles` is a subset of `$haystack`, `false` otherwise.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if `$haystack` or `$needles` is neither traversable nor an array.

**Since**

2.0.7

### isTraversable() <span id="method-isTraversable"></span>

```php
public static function isTraversable(mixed $var): bool
```

Checks whether a variable is an array or [`Traversable`](https://www.php.net/manual/en/class.traversable.php).

This method does the same as the PHP function [is_array()](https://www.php.net/manual/en/function.is-array.php)
but additionally works on objects that implement the [`Traversable`](https://www.php.net/manual/en/class.traversable.php) interface.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$var` | `mixed` | The variable being evaluated. |

**Returns**

`bool`: Whether $var can be traversed via foreach

**Since**

2.0.8

**See also**

- <https://www.php.net/manual/en/function.is-array.php>

### keyExists() <span id="method-keyExists"></span>

```php
public static function keyExists(string|int $key, array<array-key, mixed>|ArrayAccess<array-key, mixed> $array, bool $caseSensitive = true): bool
```

Checks if the given array contains the specified key.

This method enhances the `array_key_exists()` function by supporting case-insensitive
key comparison.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|int` | The key to check |
| `$array` | `array<array-key, mixed>\|`[`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php)`<array-key, mixed>` | The array with keys to check |
| `$caseSensitive` | `bool` | Whether the key comparison should be case-sensitive |

**Returns**

`bool`: Whether the array contains the specified key

### map() <span id="method-map"></span>

```php
public static function map(array $array, string|Closure $from, string|Closure $to, string|Closure|null $group = null): array
```

Builds a map (key-value pairs) from a multidimensional array or an array of objects.

The `$from` and `$to` parameters specify the key names or property names to set up the map.
Optionally, one can further group the map according to a grouping field `$group`.

For example,

```
$array = [
    ['id' => '123', 'name' => 'aaa', 'class' => 'x'],
    ['id' => '124', 'name' => 'bbb', 'class' => 'x'],
    ['id' => '345', 'name' => 'ccc', 'class' => 'y'],
];

$result = ArrayHelper::map($array, 'id', 'name');
// the result is:
// [
//     '123' => 'aaa',
//     '124' => 'bbb',
//     '345' => 'ccc',
// ]

$result = ArrayHelper::map($array, 'id', 'name', 'class');
// the result is:
// [
//     'x' => [
//         '123' => 'aaa',
//         '124' => 'bbb',
//     ],
//     'y' => [
//         '345' => 'ccc',
//     ],
// ]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` |  |
| `$from` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) |  |
| `$to` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) |  |
| `$group` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|null` |  |

**Returns**

`array`

### merge() <span id="method-merge"></span>

```php
public static function merge(array<array-key, mixed> $a, array<array-key, mixed> ...$b): array<array-key, mixed>
```

Merges two or more arrays into one recursively.

If each array has an element with the same string key value, the latter
will overwrite the former (different from array_merge_recursive).
Recursive merging will be conducted if both arrays have an element of array
type and are having the same key.
For integer-keyed elements, the elements from the latter array will
be appended to the former array.
You can use [`UnsetArrayValue`](/docs/api/yii/helpers/unsetarrayvalue/index.md) object to unset value from previous array or
[`ReplaceArrayValue`](/docs/api/yii/helpers/replacearrayvalue/index.md) to force replace former value instead of recursive merging.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$a` | `array<array-key, mixed>` | Array to be merged to |
| `...$b` | `array<array-key, mixed>` | Array to be merged from. You can specify additional arrays via third argument, fourth argument etc. |

**Returns**

`array<array-key, mixed>`: The merged array (the original arrays are not changed.)

### multisort() <span id="method-multisort"></span>

```php
public static function multisort(array &$array, string|Closure|array $key, int|array $direction = \yii\helpers\SORT_ASC, int|array $sortFlag = \yii\helpers\SORT_REGULAR): mixed
```

Sorts an array of objects or arrays (with the same structure) by one or several keys.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array to be sorted. The array will be modified after calling this method. |
| `$key` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|array` | The key(s) to be sorted by. This refers to a key name of the sub-array elements, a property name of the objects, or an anonymous function returning the values for comparison purpose. The anonymous function signature should be: `function($item)`. To sort by multiple keys, provide an array of keys here. |
| `$direction` | `int\|array` | The sorting direction. It can be either `SORT_ASC` or `SORT_DESC`. When sorting by multiple keys with different sorting directions, use an array of sorting directions. |
| `$sortFlag` | `int\|array` | The PHP sort flag. Valid values include `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`. Please refer to [PHP manual](https://www.php.net/manual/en/function.sort.php) for more details. When sorting by multiple keys with different sort flags, use an array of sort flags. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the $direction or $sortFlag parameters do not have correct number of elements as that of $key.

### recursiveSort() <span id="method-recursiveSort"></span>

```php
public static function recursiveSort(array &$array, callable|null $sorter = null): array
```

Sorts array recursively.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | An array passing by reference. |
| `$sorter` | `callable\|null` | The array sorter. If omitted, sort index array by values, sort assoc array by keys. |

**Returns**

`array`

### remove() <span id="method-remove"></span>

```php
public static function remove(array &$array, string $key, mixed $default = null): mixed|null
```

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.

Usage examples,

```
// $array = ['type' => 'A', 'options' => [1, 2]];
// working with array
$type = \yii\helpers\ArrayHelper::remove($array, 'type');
// $array content
// $array = ['options' => [1, 2]];
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array to extract value from |
| `$key` | `string` | Key name of the array element |
| `$default` | `mixed` | The default value to be returned if the specified key does not exist |

**Returns**

`mixed|null`: The value of the element if found, default value otherwise

### removeValue() <span id="method-removeValue"></span>

```php
public static function removeValue(array &$array, mixed $value): array
```

Removes items with matching values from the array and returns the removed items.

Example,

```
$array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson'];
$removed = \yii\helpers\ArrayHelper::removeValue($array, 'Jackson');
// result:
// $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger'];
// $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson'];
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array where to look the value from |
| `$value` | `mixed` | The value to remove from the array |

**Returns**

`array`: The items that were removed from the array

**Since**

2.0.11

### setValue() <span id="method-setValue"></span>

```php
public static function setValue(array &$array, string|array|null $path, mixed $value): mixed
```

Writes a value into an associative array at the key path specified.

If there is no such key path yet, it will be created recursively.
If the key exists, it will be overwritten.

```
 $array = [
     'key' => [
         'in' => [
             'val1',
             'key' => 'val'
         ]
     ]
 ];
```

The result of `ArrayHelper::setValue($array, 'key.in.0', ['arr' => 'val']);` will be the following:

```
 [
     'key' => [
         'in' => [
             ['arr' => 'val'],
             'key' => 'val'
         ]
     ]
 ]

```

The result of
`ArrayHelper::setValue($array, 'key.in', ['arr' => 'val']);` or
`ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);`
will be the following:

```
 [
     'key' => [
         'in' => [
             'arr' => 'val'
         ]
     ]
 ]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$array` | `array` | The array to write the value to |
| `$path` | `string\|array\|null` | The path of where do you want to write a value to `$array` the path can be described by a string when each key should be separated by a dot you can also describe the path as an array of keys if the path is null then `$array` will be assigned the `$value` |
| `$value` | `mixed` | The value to be written |

**Returns**

`mixed`

**Since**

2.0.13

### toArray() <span id="method-toArray"></span>

```php
public static function toArray(object|array|string $object, array $properties = [], bool $recursive = true): array
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$object` | `object\|array\|string` | The object to be converted into an array |
| `$properties` | `array` | A mapping from object class names to the properties that need to put into the resulting arrays. The properties specified for each class is an array of the following format: [ '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, ] |
| `$recursive` | `bool` | Whether to recursively converts properties which are objects into arrays. |

**Returns**

`array`: The array representation of the object