The object oriented approach to working with arrays
https://github.com/petrgrishin/array-map
Installation ¶
Add a dependency to your project's composer.json:
{
"require": {
"petrgrishin/array-map": "~1.0"
}
}
Usage examples ¶
Map ¶
Using keys
$array = ArrayMap::create($array)
->map(function ($value, $key) {
return array($key => $value);
})
->getArray();
Simple
$array = ArrayMap::create($array)
->map(function ($value) {
return $value;
})
->getArray();
Merge ¶
Recursive merge
$array = ArrayMap::create($array)
->mergeWith(array(
1 => 1,
2 => 2,
3 => array(
1 => 1,
2 => 2,
),
))
->getArray();
One level merge
$array = ArrayMap::create($array)
->mergeWith(array(
1 => 1,
2 => 2,
), false)
->getArray();
Filtering ¶
$array = ArrayMap::create($array)
->filter(function ($value, $key) {
return $value > 10 && $key > 2;
})
->getArray();
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.