Lightweight php package to work with arrays like (but fitted to PHP) in JavaScript or C#, so you can for example, chain methods. Immutable-first. Collections-like.
Project was built having in mind, that this library has to:
- be lightweight
- be fast
- be full tested
- be strict typed for better security
- have documentation
- be like JavaScript/C# array object
Install the latest version with
$ composer require dragk/array-class
<?php
use DragK\ArrayClass;
$array = new ArrayClass([1, 3, 2, 4]);
$result = $array
->sort()
->reverse()
->map(function($value){
return $value**2;
})
->filter(function($value) {
return $value > 8 ;
})
->reduce(function($result, $value) {
return $result + $value;
});
var_dump($result); // int(25)
echo $array[1]; // 3
Remember, you can always use function () use ($var1, $varN)
to pass to function more variables, for example.:
$multiplier = 2;
$result = $array
->sort()
->reverse()
->map(function($value){
return $value**2;
})
->filter(function($value) {
return $value > 8 ;
})
->reduce(function($result, $value) use ($multiplier) {
return ($result + $value) * $multiplier;
});
echo $result; // 82
- PHP 7.0 or above
Bugs, feature request and code style/approach hints are tracked on GitHub
ArrayClass is licensed under the MIT License - see the LICENSE file for details
This library is inspired by JavaScript Array Object, although most concepts have been adjusted to fit to the PHP world.