https://github.com/yiisoft/arrays
Yii Array Helper
https://github.com/yiisoft/arrays
array arrays hacktoberfest yii3
Last synced: 23 days ago
JSON representation
Yii Array Helper
- Host: GitHub
- URL: https://github.com/yiisoft/arrays
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2019-04-18T11:01:58.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-10T07:07:53.000Z (about 2 months ago)
- Last Synced: 2025-05-07T07:26:01.155Z (about 1 month ago)
- Topics: array, arrays, hacktoberfest, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 396 KB
- Stars: 52
- Watchers: 20
- Forks: 28
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
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 or higher.
## 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)