PHPackages                             alexrili/phmap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alexrili/phmap

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alexrili/phmap
==============

Payload maps and transform. From A structure to B structure

v1.1.3(3y ago)26401MITPHPPHP &gt;=8.1

Since Dec 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/alexrili/phmap)[ Packagist](https://packagist.org/packages/alexrili/phmap)[ Docs](https://github.com/alexrili/phmap)[ RSS](/packages/alexrili-phmap/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

Phmap
=====

[](#phmap)

This is just a payload map written in php. This lib helps you to get values from A structure and put it on B struscture

[![Screenshot 2022-12-04 at 02 09 39](https://user-images.githubusercontent.com/1238430/205475678-9fde3bca-e07f-4e4d-a388-a846f586816b.png)](https://user-images.githubusercontent.com/1238430/205475678-9fde3bca-e07f-4e4d-a388-a846f586816b.png)

**Note:** Phmap uses `illuminate/support` under the hood. If you are using laravel or lumen framework, make sure that you version is **&gt;= 9.x**, if you're not using any of these frameworks or this dependency(illuminate/support) , than you have nothing to worry about.

Install
-------

[](#install)

Via Composer

```
$ composer require alexrili/phmap
```

Basic usage
-----------

[](#basic-usage)

Under the hood we provide a `payload_map()` helper function. The `payload_map()` accpets two arguments: 1º is your input data, 2º is your map config. Both needs to be array type. Let`s see the example bellow.

```
#can be a response from api request
$inputData = [
    'a' => 'Value from A'
];

$map = [
    'from' => 'a',
    'to' => 'b'
];

$response = payload_map($inputData, $map);
```

```
// var_dump($response);
// return will be
array:1 [
    "b" => "Value from A"
]

```

Now let's imagine that you have a multliple values from an array and needs to change the inside properties. With **Phmap** you can do that :)

```
# e.g: Imagine that you have the following payload

$oldPayload = [
    'addresses' => [
        [
            'mainAddress' => true,
            'address' => '54 St',
            'code' => '676329-098'
        ],
        [
            'mainAddress' => false,
            'address' => 'Saint Louis Av',
            'code' => '4432-098'
        ]
    ]
];

$map = [
    [
        'from' => 'addresses.*.address',
        'to' => 'users_addresses.*.street_name'
    ],
    [
        'from' => 'addresses.*.code',
        'to' => 'users_addresses.*.postal_code'
    ]
];

$newPayload = payload_map($inputData, $map);
```

```
// var_dump($newPayload);
// return will be
array:2 [
     "users_addresses" => [
        [
            "street_name" => "54 St",
            "postal_code" => "676329-098"
        ],
        [
            "street_name" => "Saint Louis Av",
            "postal_code" => "4432-098
        ]
    ]
]

```

> Notice: The `mainAddress` property was not in the new payload, this is happening because we're not tells the payload map to map that

Map configs
-----------

[](#map-configs)

### Direct values

[](#direct-values)

> you can map a value from a direct path in your structure just point where the values are in **from** key and point the destination path within **to** key

```
[
    "from" =>"a",
    "to" => "b"
]
```

### Concatanated values

[](#concatanated-values)

> you can concatanate one or more values using the `.+.` symbol

```
[
    "from" =>"a.a1.+.a.a2",
    "to" => "b"
]
```

### Fixed values

[](#fixed-values)

> you can add some fixed values using the`__()__` symbol

```
[
    "from" =>"__(This is a fixed value)__",
    "to" => "b"
]
```

### Collection(array) of values

[](#collectionarray-of-values)

> you can map a nested property inside an array using the `.*.` symbol

```
[
    "from" =>"a.*.a1",
    "to" => "b.*.b1"
]
```

### One Or Antoher value

[](#one-or-antoher-value)

> you can map two(or more) direferente paths to bring a value, then the phmap will go through all the paths until it finds a value. To make one or another value, you can use `||` symbol

```
[
    "from" =>"a||a1",
    "to" => "b" // if finds value in [a], brings [a], if not try to brings [b]
]
```

### Nullable values

[](#nullable-values)

> you may need to map some properties that has a null value, for that you just need to pass a string flag called `nullable` with one of this two values \[`"true"`|`"false"`\], if trues, means you want to bring this property to you new structure even if their value is null.
>
> Default value is `"false"`

```
[
    "from" =>"a",
    "to" => "b"
    "nullable" => "true"
    // if [a] don't have value
    // the results will be ["b"=>null]
]
```

Advanced usage
--------------

[](#advanced-usage)

```
$inputData = [
    'a' => [
        [
            'ac1' => 'I`m a multilevel value'
        ],
        [
            'ac1' => 'I`m a multilevel value',
        ]
    ],
    'aDV' => 'I`m a directed value'
];

$map = [
    [
        'from' => 'a.*.ac1.+.__(I`m a fixed value)__.+.aDV',
        'to' => 'b.*.bc'
    ]
];

$response = payload_map($inputData, $map);
```

```
// var_dump($response)
// result wil be
array:1 [
  "b" => array:2 [
    0 => array:1 [
      "bc" => "I`m a multilevel valueI`m a fixed valueI`m a directed value"
    ]
    1 => array:1 [
      "bc" => "I`m a multilevel valueI`m a fixed valueI`m a directed value"
    ]
  ]
]

```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email alexrili instead of using the issue tracker.

Credits
-------

[](#credits)

- \[Alex Ribeiro\]\[link-author\]
- \[All Contributors\]\[link-contributors\]

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

4

Last Release

1248d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6ddf3f4bf799ee959bf1cca781d67a3dd0442b5992ac5af495d6921af961926b?d=identicon)[alexrili](/maintainers/alexrili)

---

Top Contributors

[![alexrili](https://avatars.githubusercontent.com/u/1238430?v=4)](https://github.com/alexrili "alexrili (5 commits)")

---

Tags

from-a-to-bpayload-mapmappayloadA to Bpayloadmap

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alexrili-phmap/health.svg)

```
[![Health](https://phpackages.com/badges/alexrili-phmap/health.svg)](https://phpackages.com/packages/alexrili-phmap)
```

###  Alternatives

[spatie/geocoder

Geocoding addresses to coordinates

8404.8M15](/packages/spatie-geocoder)[dasprid/enum

PHP 7.1 enum implementation

379146.0M11](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[cornford/googlmapper

An easy way to integrate Google Maps with Laravel.

457447.9k4](/packages/cornford-googlmapper)[spatie/google-time-zone

Get time zones for coordinates

110660.6k1](/packages/spatie-google-time-zone)[mostafaznv/nova-map-field

Map Field for Laravel Nova

4693.4k](/packages/mostafaznv-nova-map-field)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
