PHPackages                             apantle/hashmapper - 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. apantle/hashmapper

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

apantle/hashmapper
==================

Simple Ordered Hashmap Object Transformer (array\_map for associative arrays)

1.3.2(5y ago)01.2k[6 issues](https://github.com/apantle/hashmapper/issues)[2 PRs](https://github.com/apantle/hashmapper/pulls)2MITPHPPHP ~7 || ~8CI failing

Since Jan 3Pushed 1w agoCompare

[ Source](https://github.com/apantle/hashmapper)[ Packagist](https://packagist.org/packages/apantle/hashmapper)[ RSS](/packages/apantle-hashmapper/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (3)Versions (16)Used By (2)

[![Build Status](https://camo.githubusercontent.com/036f212f44bb47db2d60d63a817a307e49f4b876184c538672232de5c9055b21/68747470733a2f2f7472617669732d63692e6f72672f6170616e746c652f686173686d61707065722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/apantle/hashmapper) [![Maintainability](https://camo.githubusercontent.com/717cd176ec6ac937a6d258799a5bc978348ebbf239ffce7f0178d633b2ec2ad7/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35666136313362633062383765353937356136612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/apantle/hashmapper/maintainability) [![Test Coverage](https://camo.githubusercontent.com/1307106d893ac463aaa71accae2c8dc9bbc0f2b86b80658f8e62b7230fd67082/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35666136313362633062383765353937356136612f746573745f636f766572616765)](https://codeclimate.com/github/apantle/hashmapper/test_coverage)

Objective
=========

[](#objective)

This is a minimalistic library aimed to reuse logic on HashTables mapping, that i use over and over i.e. consuming API results to pass to Twig views.

Makes easy to do filtering/renaming of unwanted keys through simple dictionary of keys of source to target. Through callbacks supports any transformation, and passing it another callable or another instance of HashMapper, pretty complex transformation of associative arrays, a.k.a as Hashmaps for friends.

```
use function Apantle\HashMapper\hashMapper;

$target = hashMapper($specs, $options)($source);
```

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

[](#installation)

You can install the package via composer:

```
composer require apantle/hashmapper
```

Simple key mapping
------------------

[](#simple-key-mapping)

Change one key in input, only output that in target.

InputMapperOutput`['origin' => 'Africa'];``hashMapper(['origin' => 'roots]) ``['roots' => 'Africa']`Callback key mapping
--------------------

[](#callback-key-mapping)

For somewhat complex transforms, you can use a function that will receive as arguments:

- the value of the source hashmap specified at key:
- the whole hashmap if you need other values of it

```
assert(hashMapper([
    'place' => 'Caso CIDH',
    'date' => [
        'fecha',
        function($date, $source) {
            extract($date);
            $date = date_create_from_format('Y/m/d', "{$year}/{$month}/{$day}");
            return $date->format('Y-m-d');
        }
    ],
])([
    'date' => [
        'year' => 2006,
        'month' => 5,
        'day' => 4,
    ],
    'place' => 'San Salvador Atenco',
]) === [
    'Caso CIDH' => 'San Salvador Atenco',
    'fecha' => '2006-05-04'
]);
```

Use another HashMapper
----------------------

[](#use-another-hashmapper)

If you have a complex subkey that is not easily mapped with a simple function, you could use another HashMapper with the spec for that subkey, as the mapper for that key.

```
assert(hashMapper([
    'sourceKey' => hashMapper([ 'value' => 'legend' ])
])([
    'sourceKey' => [
        'value' => 'to pass to HashMapper',
        'ignored' => 'it should not appear'
    ],
]) === [
    'sourceKey => [
        'legend' => 'to pass to HashMapper'
    ]
]);
```

Spread Operator Mapping with Callable
-------------------------------------

[](#spread-operator-mapping-with-callable)

If you want to take a key with subkeys of the source and *spread it* (copy the dictionary of key and values it contains) on the target hashmap, you can pass a tuple with the string `'...'` as the target key, and your chosen callable.

```
assert(hashMapper([
    'wp:term' => ['...', 'Apantle\FunPHP\identity']
])([
    'wp:term' => [
         'id' => 31925,
         'link' => 'http://example.com/category/test-term/',
         'name' => 'Test term',
         'slug' => 'test-term',
         'taxonomy' => 'category',
    ],
    'ignored' => 'right'
]) === [
     'id' => 31925,
     'link' => 'http://example.com/category/test-term/',
     'name' => 'Test term',
     'slug' => 'test-term',
     'taxonomy' => 'category',
]);
```

Implicit spread (not specifying '...' key)
------------------------------------------

[](#implicit-spread-not-specifying--key)

If you need all the dictionaries inside top level keys be spread into the target, rather than writing your mapping spec as a tuple, you can give it only a callable, specifying the option `implicitSpread => true` in the constructor to the Mapper functor.

```
assert(hashMaper(
    [
        'wp:term' => compose('Apantle\FunPHP\head', 'Apantle\FunPHP\identity'),
    ],
    [
        'implicitSpread' => true
    ]
)([
    'wp:term' => [
        [
            'id' => 31925,
            'link' => 'http://example.com/category/test-term/',
            'name' => 'Test term',
            'slug' => 'test-term',
            'taxonomy' => 'category',
        ]
    ],
]) === [
    'id' => 31925,
    'link' => 'http://example.com/category/test-term/',
    'name' => 'Test term',
    'slug' => 'test-term',
    'taxonomy' => 'category',
]);
```

### Call a HashMapper as Functor object

[](#call-a-hashmapper-as-functor-object)

For better reuse, now offers through the `__invoke` magic, a simpler way to use it to map a collection of associative arrays, as `array_map`, `array_reduce` or `Collection::map` (from `Illuminate\Support`).

### Reuse a HashMapper to transform an array of associative arrays

[](#reuse-a-hashmapper-to-transform-an-array-of-associative-arrays)

Instead of using the HashMapper as the function for `array_map` or `Collection::map`, you can use our own helper, that applies the same set of transformations to every array passed.

```
$collectionTransformed = collection(hashMapper($specs))($arrayOfAssociativeArrays);
```

See [issue:1](https://github.com/apantle/hashmapper/issues/1) for more complete examples in test sources.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance64

Regular maintenance activity

Popularity14

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 91.3% 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 ~85 days

Total

12

Last Release

2113d ago

Major Versions

0.9.2 → 1.02019-08-16

PHP version history (3 changes)0.9PHP ~5.6||~7.0

1.1PHP ^7.0

1.3.1PHP ~7 || ~8

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f01afc6e15ca1ba483cc1773c5f5d1b6c5f5fa98e0fe224ae8410048eb84041?d=identicon)[jefrancomix](/maintainers/jefrancomix)

---

Top Contributors

[![tzkmx](https://avatars.githubusercontent.com/u/11432557?v=4)](https://github.com/tzkmx "tzkmx (42 commits)")[![tezcatl](https://avatars.githubusercontent.com/u/188753?v=4)](https://github.com/tezcatl "tezcatl (4 commits)")

---

Tags

associative-arraysdecorator-patternhashmaphashmappermappertransformer

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/apantle-hashmapper/health.svg)

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

###  Alternatives

[mediawiki/maps

Adds various mapping features to MediaWiki

84145.0k3](/packages/mediawiki-maps)

PHPackages © 2026

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