PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 44 This week: 1All time: 10,758 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: 36 packages by
Country: Russian Federation Russian Federation
Age: 34
All time rank: 290379 in Russian Federation Russian Federation
Week rank: 106 Up7 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

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  
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  /  .github  
File Role Description
Files folder imageworkflows (1 file)

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

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

  Files folder image Files  /  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  /  tests  /  unit  
File Role Description
  Plain text file ArrayMapperTest.php Class Class source

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:44
This week:1
All time:10,758
This week:560Up
User Comments (2)
Good Practise
0 years ago (?smail Çilo?lu)
70%StarStarStarStar
Good Practise
0 years ago (?smail Çilo?lu)
70%StarStarStarStar