PHPackages                             salamanderbe/array-mapper - 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. salamanderbe/array-mapper

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

salamanderbe/array-mapper
=========================

Simple mapper to map an object to an array based on a configuration

0.1.3(5y ago)05MITPHPPHP ^7.2

Since Oct 16Pushed 5y ago2 watchersCompare

[ Source](https://github.com/salamanderbe/array-mapper)[ Packagist](https://packagist.org/packages/salamanderbe/array-mapper)[ RSS](/packages/salamanderbe-array-mapper/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

An easy and simple way to map objects to arrays
===============================================

[](#an-easy-and-simple-way-to-map-objects-to-arrays)

Installation
------------

[](#installation)

```
composer require salamanderbe/array-mapper

```

Examples
--------

[](#examples)

### Basic example

[](#basic-example)

Given a simple object with the following values (JSON):

```
{
    "identifier": "1",
    "title": "my basic object"
}

```

When we pass this as an object (e.g. json\_decode) to the map function with the following mapping configuration:

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
];

```

The function will map the source oject's *identifier* field to the output's *id* field, the same goes for *title* and *name*. resulting in the following array:

```
[
    'id' => '1',
    'name' => 'my basic object'
]

```

### Fixed values

[](#fixed-values)

Starting with the following source object:

```
{
    "identifier": "1",
    "title": "my basic object"
}

```

With the `#` notation we can easily add a fixed value not present on the source object:

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'type' => '#my type',
];

```

Now we have an array containing our fixed value:

```
[
    'id' => '1',
    'name' => 'my basic object',
    'type' => 'my type',
]

```

*note: if your source field contains a `#` character you can escape it by adding another `#` character e.g. `##type`*

### Nesting

[](#nesting)

Using the same object as above:

```
{
    "identifier": "1",
    "title": "my basic object"
}

```

We can easily create nested mappings using the a config like the following:

```
$mapping = [
    'id' => 'identifier',
    'nested' => [
        'name' => 'title',
    ]
];

```

This will result in the following array:

```
[
    'id' => '1',
    'nested' => [
        'name' => 'my basic object'
    ]
]

```

### Nested object fields on the source object

[](#nested-object-fields-on-the-source-object)

When your source object has a child object:

```
{
    "identifier": "1",
    "title": "my basic object",
    "child": {
        "child_name": "my child"
    }
}

```

You can use the `.` notation to access child fields, like the following example config:

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'child_name' => 'child.child_title',
];

```

The child's *child\_title* field will now be mapped to output's *child\_name* field resulting in the following output:

```
[
    'id' => '1',
    'name' => 'my basic object',
    'child_name' => 'my child'
]

```

### Arrays on the source object

[](#arrays-on-the-source-object)

Given the following object:

```
{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ]
}

```

When we want to map fields contained in child objects we can use the `*` notation to indicate we want a field from an array of objects, for example:

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'child_names' => 'children.*.child_title',
];

```

Now the *child\_names* field on the output array will contain all the child's *child\_title* fields:

```
[
    'id' => '1',
    'name' => 'my basic object',
    'child_names' => [
        'my first child',
        'my second child'
    ]
]

```

### Arrays in the result array

[](#arrays-in-the-result-array)

Say you want to map the children into a nested array:

```
{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ]
}

```

We can use the `.*` notation on the output field to indicate we want to map multiple children's fields

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'children.*' => [
        'name' => 'children.*.child_title',
    ],
];

```

Now all the source objects' children *child\_title* fields will be mapped to a *children* array on the output array, each item inside *children* will be an array with the child's title field mapped to the output's name field:

```
[
    'id' => '1',
    'name' => 'my basic object',
    'children' => [
        [
            'name' => 'my first child'
        ],
        [
            'name' => 'my second child'
        ],

    ]
]

```

### Merging arrays in the result array

[](#merging-arrays-in-the-result-array)

Let's assume we have a source object with 2 array which we want to be combined in a single result:

```
{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ],
    "grandchildren": [
        {
            "grandchild_title": "my first grandchild"
        },
        {
            "grandchild_title": "my second grandchild"
        }
    ]
}

```

We can combine these using the same syntax as with the arrays but instead nesting multiple sources:

```
$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'descendants.*' => [
        ['name' => 'children.*.child_title'],
        ['name' => 'grandchildren.*.grandchild_title'],
    ],
];

```

This will result in a result array with a *descendants* field containing both the source's *children* and *grandchildren*:

```
[
    'id' => '1',
    'name' => 'my basic object',
    'descendants' => [
        [
            'name' => 'my first child'
        ],
        [
            'name' => 'my second child'
        ],
        [
            'name' => 'my first grandchild'
        ],
        [
            'name' => 'my second grandchild'
        ],
    ]
]

```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

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 ~2 days

Total

4

Last Release

2034d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/13162345b34a6b54231e902eb0dfb49795e8eec299f1cd61f1934ec26aa2cd41?d=identicon)[salamander.be](/maintainers/salamander.be)

![](https://www.gravatar.com/avatar/051747449d14fce8d12415cb78bccf4a99ab333bdfd78095ae1602e1b138073b?d=identicon)[Gration](/maintainers/Gration)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/salamanderbe-array-mapper/health.svg)

```
[![Health](https://phpackages.com/badges/salamanderbe-array-mapper/health.svg)](https://phpackages.com/packages/salamanderbe-array-mapper)
```

###  Alternatives

[felipebool/crook

Crook is a simple tool to ease your life when dealing with git hooks

5813.8k](/packages/felipebool-crook)[minube/amplitude-php

Amplitude SDK for PHP - Use Amplitude in your PHP project

1455.7k](/packages/minube-amplitude-php)

PHPackages © 2026

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