PHPackages                             boesing/typed-arrays - 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. boesing/typed-arrays

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

boesing/typed-arrays
====================

Hashmap and Collection

1.3.0(1y ago)1036.5k↓39.1%3[6 issues](https://github.com/boesing/typed-arrays/issues)[2 PRs](https://github.com/boesing/typed-arrays/pulls)1MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0CI passing

Since Oct 17Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/boesing/typed-arrays)[ Packagist](https://packagist.org/packages/boesing/typed-arrays)[ RSS](/packages/boesing-typed-arrays/feed)WikiDiscussions 1.4.x Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (52)Used By (1)

typed-arrays
============

[](#typed-arrays)

---

[![Continuous Integration](https://github.com/boesing/typed-arrays/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/boesing/typed-arrays/actions/workflows/continuous-integration.yml)[![type-coverage](https://camo.githubusercontent.com/448b3f25e19ca2e2bc9159ea634ccddfeaf7ad21038d503ac9ef40b8aaae1a27/68747470733a2f2f73686570686572642e6465762f6769746875622f626f6573696e672f74797065642d6172726179732f636f7665726167652e737667)](https://shepherd.dev/github/boesing/typed-arrays)[![psalm](https://camo.githubusercontent.com/4164a44492b2171713ea70f4f809a2f2959f65f88f71418385015372dc667462/68747470733a2f2f73686570686572642e6465762f6769746875622f626f6573696e672f74797065642d6172726179732f6c6576656c2e737667)](https://shepherd.dev/github/boesing/typed-arrays)

Totally typed library to work with lists or maps.

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

[](#installation)

To use this library in your project, please install it via composer:

```
$ composer require boesing/typed-arrays
```

Usage
-----

[](#usage)

The main reason why this library was created was the fact, that *every* array in PHP is a hashmap. If you primarily work with APIs, you might have experienced that `json_encode` of an array type sometimes leads to annoying issues.

To get rid of `array` being passed through an application, the `OrderedListInterface` and the `MapInterface` became very handy. To also provide most if not any of the `array_*` functions to the developers, most of these array functions do have a method within `OrderedListInterface` or `MapInterface`.

### Common mistakes

[](#common-mistakes)

Lets take some real-world use cases to better reflect the idea behind this library:

```
$listOfIntegers = [1, 2, 3, 4];

$myObject = new stdClass();
$myObject->integers = $listOfIntegers;

echo json_encode($myObject) . PHP_EOL;

// Output of the code above will be: `{"integers":[1,2,3,4]}`
// Now some refactoring has to be made since the requirement changed. The requirement now is that the integers list
// must not contain odd values anymore. So `array_filter` to the rescue, right?

$listOfEvenIntegers = array_filter([1, 2, 3, 4], static fn (int $integer): int => $integer % 2 === 0);

$myObject = new stdClass();
$myObject->integers = $listOfEvenIntegers;

echo json_encode($myObject) . PHP_EOL;

// Output of the refactored code above now became: `{"integers":{"1":2,"3":4}}`
// So what now happened is a huge problem for highly type-sensitive API clients since we changed a list to a hashmap
// Same happens with hashmaps which suddenly become empty.

$hashmap = [
    'foo' => 'bar',
];
$myObject = new stdClass();
$myObject->map = $hashmap;

echo json_encode($myObject) . PHP_EOL;

// Output of the code above will be: `{"map":{"foo":"bar"}}`
// So now some properties are being added, some are being removed, the definition of your API says
// "the object will contain additional properties because heck I do not want to declare every property"
// "so to make it easier, every property has a string value"
// can be easily done with something like this in JSONSchema: `{"type": "object", "additional_properties": {"type": "string"}}`
// Now, some string value might become `null` due to whatever reason, lets say it was a bug and thus the happy path always returned a string
// The most logical way here is, due to our lazyness, to use something like `array_filter` to get rid of all our non-string values

$hashmap = [
    'foo' => null,
];
$myObject = new stdClass();
$myObject->map = array_filter($hashmap);

echo json_encode($myObject) . PHP_EOL;

// Output of the refactored code above now became: `{"map":[]}`
// So in case that every array value is being wiped due to the filtering, we suddenly have a type-change from
// a hashmap to a list. This is ofc also problematic since we do not want to have a list here but an empty object like
// so: `{"map":{}}`
```

*(The above example can be verified on 3v4l.org - a PHP sandbox: )*

### typed-arrays to the rescue

[](#typed-arrays-to-the-rescue)

So with this library, one is a little bit more type-safe when it comes to array handling. However, the `MapInterface` actually will become `null` within a `json_encode` in case it is empty.

So lets take the above example in combination with our factories:

```
use Boesing\TypedArrays\TypedArrayFactory;
$factory = new TypedArrayFactory();

$listOfIntegers = $factory->createOrderedList([1, 2, 3, 4]);

$myObject = new stdClass();
$myObject->integers = $listOfIntegers;

echo json_encode($myObject) . PHP_EOL;

// Output of the code above will be: `{"integers":[1,2,3,4]}`
// Now some refactoring has to be made since the requirement changed. The requirement now is that the integers list
// must not contain odd values anymore. So `array_filter` to the rescue, right?

$listOfEvenIntegers = $factory->createOrderedList([1, 2, 3, 4])->filter(static fn (int $integer): int => $integer % 2 === 0);

$myObject = new stdClass();
$myObject->integers = $listOfEvenIntegers;

echo json_encode($myObject) . PHP_EOL;

// Output of the refactored code above now became: `{"integers":[2, 4]}`
// Due to the internal handling of `array_filter`, the `OrderedListInterface` won't change its type.

// Even hashmaps can be filtered, the type stays the same but in case of an empty map, `null` is being passed to the JSON object
$hashmap = $factory->createMap([
    'foo' => 'bar',
]);
$myObject = new stdClass();
$myObject->map = $hashmap;

echo json_encode($myObject) . PHP_EOL;

// Output of the code above will be: `{"map":{"foo":"bar"}}`
// So now some properties are being added, some are being removed, the definition of your API says
// "the object will contain additional properties because heck I do not want to declare every property"
// "so to make it easier, every property has a string value"
// can be easily done with something like this in JSONSchema: `{"type": "object", "additional_properties": {"type": "string"}}`
// Now, some string value might become `null` due to whatever reason, lets say it was a bug and thus the happy path always returned a string
// The most logical way here is, due to our lazyness, to use something like `array_filter` to get rid of all our non-string values

$hashmap = $factory->createMap([
    'foo' => null,
]);
$myObject = new stdClass();
$myObject->map = $hashmap->filter(static fn ($value) => $value !== null);

echo json_encode($myObject) . PHP_EOL;

// Output of the refactored code above now became: `{"map":null}`
// So in case that every array value is being wiped due to the filtering, we suddenly have a type-change from
// a hashmap to a list. This is ofc also problematic since we do not want to have a list here but an empty object like
// so: `{"map":{}}`
```

### Conclusion

[](#conclusion)

When it comes to API responses, you might not want to rely on PHP array structure. Always prefer real objects with real properties and real property type-hints over `non-empty-array`.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance49

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 65.1% 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 ~35 days

Recently: every ~68 days

Total

43

Last Release

573d ago

Major Versions

0.13.1 → 1.0.02022-05-30

1.4.x-dev → 2.0.x-dev2024-10-22

PHP version history (5 changes)0.1.0PHP ^7.3 || ~8.0.0

0.13.x-devPHP ^7.3 || ~8.0.0 || ~8.1.0

1.0.0PHP ~7.4.0 || ~8.0.0 || ~8.1.0

1.1.0PHP ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0

1.2.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/bb2738b201c0f9c9eb9a0682e72329fca6d2b8b1cf65402ed490a1e716879885?d=identicon)[boesing](/maintainers/boesing)

---

Top Contributors

[![boesing](https://avatars.githubusercontent.com/u/2189546?v=4)](https://github.com/boesing "boesing (220 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (112 commits)")[![alikallali](https://avatars.githubusercontent.com/u/129870017?v=4)](https://github.com/alikallali "alikallali (3 commits)")[![icanhazstring](https://avatars.githubusercontent.com/u/883543?v=4)](https://github.com/icanhazstring "icanhazstring (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/boesing-typed-arrays/health.svg)

```
[![Health](https://phpackages.com/badges/boesing-typed-arrays/health.svg)](https://phpackages.com/packages/boesing-typed-arrays)
```

###  Alternatives

[phpdocumentor/reflection-docblock

With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.

9.4k722.2M1.2k](/packages/phpdocumentor-reflection-docblock)[icanhazstring/composer-unused

Show unused packages by scanning your code

1.7k7.0M188](/packages/icanhazstring-composer-unused)[symplify/monorepo-builder

Not only Composer tools to build a Monorepo.

5205.3M82](/packages/symplify-monorepo-builder)[phpdocumentor/reflection

Reflection library to do Static Analysis for PHP Projects

12521.4M109](/packages/phpdocumentor-reflection)[sylius/fixtures-bundle

Configurable fixtures for Symfony applications.

517.7M12](/packages/sylius-fixtures-bundle)[sylius/promotion

Flexible promotion management for PHP applications.

28477.8k9](/packages/sylius-promotion)

PHPackages © 2026

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