The object oriented approach to working with arrays
Add a dependency to your project's composer.json:
{
"require": {
"petrgrishin/array-map": "~1.0"
}
}
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();
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();
$array = ArrayMap::create($array)
->filter(function ($value, $key) {
return $value > 10 && $key > 2;
})
->getArray();
Sort by value
$array = ArrayMap::create($array)
->userSortByValue(function ($first, $second) {
return $first < $second ? -1 : 1;
})
->getArray();
Sort by key
$array = ArrayMap::create($array)
->userSortByKey(function ($first, $second) {
return $first < $second ? -1 : 1;
})
->getArray();
ArrayAccess class, multi array access — https://github.com/petrgrishin/array-access