# arrays
**Repository Path**: mirrors_yiisoft/arrays
## Basic Information
- **Project Name**: arrays
- **Description**: Yii Array Helper
- **Primary Language**: Unknown
- **License**: BSD-3-Clause
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-08-19
- **Last Updated**: 2026-02-14
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Yii Arrays
[](https://packagist.org/packages/yiisoft/arrays)
[](https://packagist.org/packages/yiisoft/arrays)
[](https://github.com/yiisoft/arrays/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/arrays)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/arrays/master)
[](https://github.com/yiisoft/arrays/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/arrays)
The package provides:
- `ArrayHelper` that has static methods to work with arrays;
- `ArraySorter` that has static methods for sort arrays;
- `ArrayAccessTrait` provides the implementation for
[\IteratorAggregate](https://www.php.net/manual/class.iteratoraggregate),
[\ArrayAccess](https://www.php.net/manual/class.arrayaccess) and
[\Countable](https://www.php.net/manualn/class.countable.php);
- `ArrayableInterface` and `ArrayableTrait` for use in classes who want to support customizable representation of their instances.
## Requirements
- PHP 8.1 - 8.5.
## Installation
```shell
composer require yiisoft/arrays
```
## ArrayHelper usage
Array helper methods are static so usage is like the following:
```php
$username = \Yiisoft\Arrays\ArrayHelper::getValue($_POST, 'username');
```
Overall the helper has the following method groups.
### Getting data
- getValue
- getValueByPath
- getColumn
- getObjectVars
### Setting data
- addValue
- addValueByPath
- setValue
- setValueByPath
### Removing data
- remove
- removeByPath
- removeValue
### Detecting array types
- isIndexed
- isAssociative
### HTML encoding and decoding values
- htmlDecode
- htmlEncode
### Testing against arrays
- isIn
- isSubset
### Transformation
- index
- group
- filter
- map
- merge
- parametrizedMerge
- renameKey
- toArray
### Other
- keyExists
- pathExists
## ArraySorter usage
Array sorter has one static method which usage is like the following:
```php
\Yiisoft\Arrays\ArraySorter::multisort($data, ['age', 'name'], [SORT_ASC, SORT_DESC]);
```
## ArrayAccessTrait usage
`ArrayAccessTrait` provides the implementation for
[\IteratorAggregate](https://www.php.net/manual/class.iteratoraggregate),
[\ArrayAccess](https://www.php.net/manual/class.arrayaccess) and
[\Countable](https://www.php.net/manualn/class.countable.php).
Note that `ArrayAccessTrait` requires the class using it contain a property named `data` which should be an array.
The data will be exposed by ArrayAccessTrait to support accessing the class object like an array.
Example of use:
```php
use \Yiisoft\Arrays\ArrayAccessTrait;
class OfficeClassification implements \IteratorAggregate, \ArrayAccess, \Countable
{
use ArrayAccessTrait;
public array $data = [
'a' => 'Class A',
'b' => 'Class B',
'c' => 'Class C',
];
}
$classification = new OfficeClassification();
echo 'Count classes: ' . $classification->count() . "\n"; // 3
$iterator = $classification->getIterator();
while ($iterator->valid()) {
echo $iterator->current() . "\n"; // Class A, Class B, Class C
$iterator->next();
}
```
## ArrayableInterface and ArrayableTrait usage
`ArrayableInterface` and its implementation `ArrayableTrait` intended for use in classes who want to support customizable representation of their instances.
Example of use:
```php
use \Yiisoft\Arrays\ArrayableTrait;
use \Yiisoft\Arrays\ArrayableInterface;
class Car implements ArrayableInterface
{
use ArrayableTrait;
public string $type = 'Crossover';
public string $color = 'Red';
public int $torque = 472;
}
$car = new Car();
$data = $car->toArray(['type', 'color']); // ['type' => 'Crossover', 'color' => 'Red']
```
## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
## License
The Yii Arrays is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.
Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)