Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smoren/array-mapper-php
Helper for mapping arrays
https://github.com/smoren/array-mapper-php
array helper mapping php
Last synced: about 1 month ago
JSON representation
Helper for mapping arrays
- Host: GitHub
- URL: https://github.com/smoren/array-mapper-php
- Owner: Smoren
- License: mit
- Created: 2022-07-22T09:33:50.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-09-07T11:15:54.000Z (over 2 years ago)
- Last Synced: 2024-05-03T14:15:51.855Z (8 months ago)
- Topics: array, helper, mapping, php
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# array-mapper
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/array-mapper)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/array-mapper-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/array-mapper-php/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/Smoren/array-mapper-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/array-mapper-php?branch=master)
![Build and test](https://github.com/Smoren/array-mapper-php/actions/workflows/test_master.yml/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)Helper for mapping arrays
### How to install to your project
```
composer require smoren/array-mapper
```### Unit testing
```
composer install
./vendor/bin/codecept build
./vendor/bin/codecept run unit tests/unit
```### Usage
```php
use Smoren\ArrayMapper\ArrayMapper;$source = [
[
'id' => 1,
'country' => 'Russia',
'city' => 'Moscow',
],
[
'id' => 2,
'country' => 'Russia',
'city' => 'Moscow',
],
[
'id' => 3,
'country' => 'Russia',
'city' => 'Tomsk',
],
[
'id' => 4,
'country' => 'Belarus',
'city' => 'Minsk',
],
[
'id' => 5,
'country' => 'Belarus',
],
];$result = ArrayMapper::map($source, ['country', 'city'], true, true);
print_r($result);
/*
Array
(
[Russia] => Array
(
[Moscow] => Array
(
[0] => Array
(
[id] => 1
[country] => Russia
[city] => Moscow
)
[1] => Array
(
[id] => 2
[country] => Russia
[city] => Moscow
)
)
[Tomsk] => Array
(
[0] => Array
(
[id] => 3
[country] => Russia
[city] => Tomsk
)
)
)
[Belarus] => Array
(
[Minsk] => Array
(
[0] => Array
(
[id] => 4
[country] => Belarus
[city] => Minsk
)
)
)
)
*/$result = ArrayMapper::map($source, ['country', 'city'], true, true, function($item) {
return $item['id'];
});print_r($result);
/*
Array
(
[Russia] => Array
(
[Moscow] => Array
(
[0] => 1
[1] => 2
)
[Tomsk] => Array
(
[0] => 3
)
)
[Belarus] => Array
(
[Minsk] => Array
(
[0] => 4
)
)
)
*/$source = [
[
'id' => 1,
'country' => 'Russia',
'city' => 'Moscow',
],
[
'id' => 2,
'country' => 'Russia',
'city' => 'Moscow',
],
[
'id' => 3,
'country' => 'Russia',
'city' => 'Tomsk',
],
[
'id' => 4,
'country' => 'Belarus',
'city' => 'Minsk',
],
];$mapFields = [
'country',
function($item) {
return $item['city'].'-'.$item['id'];
}
];$result = ArrayMapper::map($source, $mapFields, false, true, function($item) {
return $item['id'];
});/*
Array
(
[Russia] => Array
(
[Moscow-1] => 1
[Moscow-2] => 2
[Tomsk-3] => 3
)
[Belarus] => Array
(
[Minsk-4] => 4
)
)
*/
```