petrgrishin / array-map
The object oriented approach to working with arrays on PHP
Installs: 5 619
Dependents: 1
Suggesters: 0
Security: 0
Stars: 12
Watchers: 5
Forks: 1
Open Issues: 1
Requires
- php: >=5.3.0
- petrgrishin/array-object: ~1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-11-01 20:40:56 UTC
README
The object oriented approach to working with arrays
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();
User sort
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();
Example of use
ArrayAccess class, multi array access — https://github.com/petrgrishin/array-access