Recommend this page to a friend! |
Helper for mapping arrays
composer require smoren/array-mapper
composer install
./vendor/bin/codecept build
./vendor/bin/codecept run unit tests/unit
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
)
)
*/
Classes of Smoren Freelight | > | PHP Array Mapping | > | Download .zip .tar.gz | > | Support forum (1) | > | Blog | > | Latest changes |
|
Groups | Applications | Files |
Groups |
Algorithms | Numerical and statistical algorithms | View top rated classes |
Data types | Modeling and manipulating data types | View top rated classes |
PHP 7 | Classes using PHP 7 specific features | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (2 files) | ||||
tests (3 files, 2 directories) | ||||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description |
---|---|---|
ArrayMapper.php | Class | Class source |
ArrayMapperException.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (1 file) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Download all files: array-mapper-php.tar.gz array-mapper-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (2 files) | ||||
tests (3 files, 2 directories) | ||||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description |
---|---|---|
ArrayMapper.php | Class | Class source |
ArrayMapperException.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (1 file) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Download all files: array-mapper-php.tar.gz array-mapper-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|