yiisoft/arrays
yiisoft/arrays is a small PHP helper library for working with arrays. It provides safe, convenient methods to get and set values (including nested paths), filter and merge data, and simplify common array operations in Yii and any PHP project.
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,
\ArrayAccess and
\Countable;ArrayableInterface and ArrayableTrait for use in classes who want to support customizable representation of their instances.composer require yiisoft/arrays
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.
Array sorter has one static method which usage is like the following:
\Yiisoft\Arrays\ArraySorter::multisort($data, ['age', 'name'], [SORT_ASC, SORT_DESC]);
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 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']
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.
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.
How can I help you explore Laravel packages today?