PHP Classes

PHP Array Mapping: Create new arrays mapping values of entry keys

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 44 All time: 10,814 This week: 560Up
Version License PHP version Categories
array-mapper-php 1.0MIT/X Consortium ...7.2Algorithms, Data types, PHP 7
Description 

Author

This package can create new arrays mapping values of entry keys.

It can take as parameters an associative array, the name of the array entry key, and the name of the entry key to be created.

The package can return the new array after copying the values of the input array keys to the new array with mapped entry keys.

It can also use callback functions to define how the array values will be mapped.

Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280978 in Russian Federation Russian Federation
Week rank: 49 Up7 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

array-mapper

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: 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

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
        )
)
*/

  Files folder image Files (12)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 files)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (12)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files (12)  /  src  
File Role Description
  Plain text file ArrayMapper.php Class Class source
  Plain text file ArrayMapperException.php Class Class source

  Files folder image Files (12)  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files (12)  /  tests  /  unit  
File Role Description
  Plain text file ArrayMapperTest.php Class Class source

  Files folder image Files (12)  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:44
This week:0
All time:10,814
This week:560Up
User Comments (2)
Good Practise
1 year ago (?smail Çilo?lu)
70%StarStarStarStar
Good Practise
1 year ago (?smail Çilo?lu)
70%StarStarStarStar