PHP Classes

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

Recommend this page to a friend!

  Author Author  
Picture of Smoren  Freelight
Name: Smoren Freelight <contact>
Classes: 16 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 6x


  Detailed description   Download Download .zip .tar.gz  
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.

Details

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

  Classes of Smoren Freelight  >  PHP Array Mapping  >  Download Download .zip .tar.gz  >  Support forum Support forum (1)  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Array Mapping
Base name: array-mapper-php
Description: Create new arrays mapping values of entry keys
Version: -
PHP version: 7.2
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image Algorithms Numerical and statistical algorithms View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Download 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 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

Download 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.