https://github.com/petrgrishin/array-map
PHP. The object oriented approach to working with arrays
https://github.com/petrgrishin/array-map
array map php
Last synced: 8 days ago
JSON representation
PHP. The object oriented approach to working with arrays
- Host: GitHub
- URL: https://github.com/petrgrishin/array-map
- Owner: petrgrishin
- Created: 2014-06-01T06:51:30.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-08-05T19:27:42.000Z (almost 7 years ago)
- Last Synced: 2024-04-23T18:15:06.594Z (about 1 year ago)
- Topics: array, map, php
- Language: PHP
- Homepage: https://github.com/petrgrishin/array-map
- Size: 18.6 KB
- Stars: 12
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
array-map
=========
[](https://travis-ci.org/petrgrishin/array-map)
[](https://coveralls.io/r/petrgrishin/array-map?branch=master)The object oriented approach to working with arrays
Installation
------------
Add a dependency to your project's composer.json:
```json
{
"require": {
"petrgrishin/array-map": "~1.0"
}
}
```Usage examples
--------------
#### Map
Using keys
```php
$array = ArrayMap::create($array)
->map(function ($value, $key) {
return array($key => $value);
})
->getArray();
```Simple
```php
$array = ArrayMap::create($array)
->map(function ($value) {
return $value;
})
->getArray();
```#### Merge
Recursive merge
```php
$array = ArrayMap::create($array)
->mergeWith(array(
1 => 1,
2 => 2,
3 => array(
1 => 1,
2 => 2,
),
))
->getArray();
```One level merge
```php
$array = ArrayMap::create($array)
->mergeWith(array(
1 => 1,
2 => 2,
), false)
->getArray();
```#### Filtering
```php
$array = ArrayMap::create($array)
->filter(function ($value, $key) {
return $value > 10 && $key > 2;
})
->getArray();
```#### User sort
Sort by value
```php
$array = ArrayMap::create($array)
->userSortByValue(function ($first, $second) {
return $first < $second ? -1 : 1;
})
->getArray();
```Sort by key
```php
$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