2017 © Pedro Peláez
 

php-library array-key-map

image

alaa/array-key-map

  • Thursday, June 14, 2018
  • by alaa-almaliki
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Array Key Map

Ever wanted to rename array keys to suit your requirements but it will take many lines that results in an ugly code? Here is a clean way to do it, (*1)

Installation

By Composer: composer require alaa/array-key-map, (*2)

Documentation

There are three types you can use to map your array keys against the keys you want your array to be mapped with., (*3)

1. Simply using an array to map keys

Example, (*4)

$array = [
    'First Name' => 'John',
    'Last Name' => 'Doe',
    'email' => 'john.doe@example.com',
    'Field Experience' => 10,
    'Current Job' => 'Software Engineer',
    'Department' => 'Web Development'
];

$mappedKeys = [
    'First Name' => 'first_name',
    'Last Name' => 'last_name',
    'Field Experience' => 'experience',
    'Current Job' => 'job',
    'Department' => 'department'
];

$sourceArray = new \ArrayKey\Map\Source\SourceArray($mappedKeys);
$source = new \ArrayKey\Map\Source($sourceArray, $array);
print_r($source->getSourceArray());
//result:
Array
(
    [First Name] => John
    [Last Name] => Doe
    [email] => john.doe@example.com
    [Field Experience] => 10
    [Current Job] => Software Engineer
    [Department] => Web Development
)

print_r($source->getMappedArray());
//result:
Array
(
    [first_name] => John
    [last_name] => Doe
    [email] => john.doe@example.com
    [experience] => 10
    [job] => Software Engineer
    [department] => Web Development
)

2. Csv Mapping to map array keys

Example:, (*5)

$array = [
    'First Name' => 'John',
    'Last Name' => 'Doe',
    'email' => 'john.doe@example.com',
    'Field Experience' => 10,
    'Current Job' => 'Software Engineer',
    'Department' => 'Web Development'
];

keys.csv
|------------------|------------|
| First Name       | first_name |
|------------------|------------|
| Last Name        | last_name  |
|------------------|------------|
| Field Experience | experience |
|------------------|------------|
| Current Job      | job        |
|------------------|------------|
| Department       | department |
|------------------|------------|

$sourceCsv = new \ArrayKey\Map\Source\SourceCsv(new \ArrayKey\Map\FileValidator(__DIR__ . '/keys.csv'));
$source = new \ArrayKey\Map\Source($sourceCsv, $array);

print_r($source->getSourceArray());
//result:
Array
(
    [First Name] => John
    [Last Name] => Doe
    [email] => john.doe@example.com
    [Field Experience] => 10
    [Current Job] => Software Engineer
    [Department] => Web Development
)

print_r($source->getMappedArray());
//result:
Array
(
    [first_name] => John
    [last_name] => Doe
    [email] => john.doe@example.com
    [experience] => 10
    [job] => Software Engineer
    [department] => Web Development
)

3. Xml Mapping to map array keys

Example:, (*6)

$array = [
    'First Name' => 'John',
    'Last Name' => 'Doe',
    'email' => 'john.doe@example.com',
    'Field Experience' => 10,
    'Current Job' => 'Software Engineer',
    'Department' => 'Web Development'
];

keys.xml

<array_map>
    <keys>
        <key>
            <origin>First Name</origin>
            <mapped>first_name</mapped>
        </key>
        <key>
            <origin>Last Name</origin>
            <mapped>last_name</mapped>
        </key>
        <key>
            <origin>Field Experience</origin>
            <mapped>experience</mapped>
        </key>
        <key>
            <origin>Current Job</origin>
            <mapped>job</mapped>
        </key>
        <key>
            <origin>Department</origin>
            <mapped>department</mapped>
        </key>
    </keys>
</array_map>

$sourceXml = new \ArrayKey\Map\Source\SourceXml(new \ArrayKey\Map\FileValidator(__DIR__ . '/keys.xml'));
$source = new \ArrayKey\Map\Source($sourceXml, $array);

print_r($source->getSourceArray());
//result:
Array
(
    [First Name] => John
    [Last Name] => Doe
    [email] => john.doe@example.com
    [Field Experience] => 10
    [Current Job] => Software Engineer
    [Department] => Web Development
)

print_r($source->getMappedArray());
//result:
Array
(
    [first_name] => John
    [last_name] => Doe
    [email] => john.doe@example.com
    [experience] => 10
    [job] => Software Engineer
    [department] => Web Development
)

Limitation

The library is made to work on single arrays only, not multidimensional arrays., (*7)

LICENSE

MIT, (*8)

The Versions

14/06 2018

dev-master

9999999-dev https://gitlab.com/alaa-almaliki/array-key-map

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Alaa Al-Maliki

php arrays array-key-map

14/06 2018

1.0.0

1.0.0.0 https://gitlab.com/alaa-almaliki/array-key-map

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Alaa Al-Maliki

php arrays array-key-map