yiisoft / arrays
Yii Array Helper
                                    Fund package maintenance!
                                                                            
                                                                                                                                        Opencollective
                                                                                    
                                                                            
                                                                                                                                        yiisoft
                                                                                    
                                                                
Installs: 1 756 708
Dependents: 81
Suggesters: 2
Security: 0
Stars: 53
Watchers: 18
Forks: 28
Open Issues: 6
pkg:composer/yiisoft/arrays
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
 - yiisoft/strings: ^2.1
 
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8.2
 - phpunit/phpunit: ^10.5.43
 - rector/rector: ^2.0.7
 - roave/infection-static-analysis-plugin: ^1.35
 - spatie/phpunit-watcher: ^1.24
 - vimeo/psalm: ^5.26.1|^6.1
 
This package is auto-updated.
Last update: 2025-10-25 11:24:27 UTC
README
Yii Arrays
The package provides:
ArrayHelperthat has static methods to work with arrays;ArraySorterthat has static methods for sort arrays;ArrayAccessTraitprovides the implementation for \IteratorAggregate, \ArrayAccess and \Countable;ArrayableInterfaceandArrayableTraitfor use in classes who want to support customizable representation of their instances.
Requirements
- PHP 8.1 or higher.
 
Installation
composer require yiisoft/arrays
ArrayHelper usage
Array helper methods are static so usage is like the following:
$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:
\Yiisoft\Arrays\ArraySorter::multisort($data, ['age', 'name'], [SORT_ASC, SORT_DESC]);
ArrayAccessTrait usage
ArrayAccessTrait provides the implementation for
\IteratorAggregate,
\ArrayAccess and
\Countable.
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:
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:
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
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Arrays is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.