PHPackages                             softboxlab/php-array-utils - 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. softboxlab/php-array-utils

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

softboxlab/php-array-utils
==========================

Lib with many array utilitaries.

v0.0.7(8y ago)08.0kMITPHP

Since Jun 16Pushed 8y agoCompare

[ Source](https://github.com/SoftboxLab/php-array-utils)[ Packagist](https://packagist.org/packages/softboxlab/php-array-utils)[ RSS](/packages/softboxlab-php-array-utils/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (4)Dependencies (1)Versions (8)Used By (0)

PHP-ARRAY-UTILS
===============

[](#php-array-utils)

[![Build Status](https://camo.githubusercontent.com/66bb5e172bf23c17e1c9f7a138b3fb942ba81ddfe3977e16a9d89594da1df6b9/68747470733a2f2f7472617669732d63692e6f72672f746172636973696f6a722f7068702d636173742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tarcisiojr/php-cast)[![codecov](https://camo.githubusercontent.com/29ae440aa1168f1cb8828956d7a10386d359fe82f7aa818c57ea4bce70d83e3d/68747470733a2f2f636f6465636f762e696f2f67682f746172636973696f6a722f7068702d636173742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/tarcisiojr/php-cast)[![Latest Stable Version](https://camo.githubusercontent.com/18de108899f2c8eeec36b8172e0677e95b102962496c01140311817baf610b4f/68747470733a2f2f706f7365722e707567782e6f72672f746172636973696f6a722f7068702d636173742f762f737461626c65)](https://packagist.org/packages/tarcisiojr/php-cast)[![Total Downloads](https://camo.githubusercontent.com/ebfa29f91493169a632aec1323c8fd7c87c7ee05787c21a1b5a1114802c93671/68747470733a2f2f706f7365722e707567782e6f72672f746172636973696f6a722f7068702d636173742f646f776e6c6f616473)](https://packagist.org/packages/tarcisiojr/php-cast)[![Latest Unstable Version](https://camo.githubusercontent.com/75020321c88a41025ff0216364f39194ba3a1f1797f9bce727f0f34c03633b73/68747470733a2f2f706f7365722e707567782e6f72672f746172636973696f6a722f7068702d636173742f762f756e737461626c65)](https://packagist.org/packages/tarcisiojr/php-cast)[![composer.lock](https://camo.githubusercontent.com/c1fd126e218203460b05cbb1939dc929e034506d38b72ecb4c823e928e3ddd13/68747470733a2f2f706f7365722e707567782e6f72672f746172636973696f6a722f7068702d636173742f636f6d706f7365726c6f636b)](https://packagist.org/packages/tarcisiojr/php-cast)[![SensioLabsInsight](https://camo.githubusercontent.com/c2127d3432716e598e998fec23c96c8f0d46092b58c817096b863982a6a33f53/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f66346333396131342d623938322d343264342d626433352d3930626636363064633439612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/f4c39a14-b982-42d4-bd35-90bf660dc49a)

Helps to cast values of array of array. Util when it is necessary garantee the type of output value, por exemple, encoding of arrays in JSON. Beyond cast values, it is possible format too.

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

[](#installation)

```
composer require tarcisiojr/php-cast

```

Usage
-----

[](#usage)

Example:

```
class Example {
    public function test() {
        $value = [
            "a" => [
                "b" => [
                    "c1" => "a",
                    "c2" => "2",
                    "c3" => "3",
                    "c4" => ["1", "2", "3"],
                    "c5" => [
                        ["d1" => "9"],
                        ["d1" => "8"],
                        ["d1" => "7"],
                    ]
                ]
            ]
        ];

        // 1. Create a instance of CastHelper
        // 2. Configure the rules to cast values
        // 3. Execute method 'cast' to cast array values

        $ret = (new CastHelper())
            ->addRule("a.b.c1", "string")
            ->addRule("a.b.c2", "int")
            ->addRule("a.b.c3", "float")
            ->addRule("a.b.c4.*", "int")
            ->addRule("a.b.c5.*.d1", "string|lpad:2,0")
            ->cast($value);

        echo "\n\n" . json_encode($ret, JSON_PRETTY_PRINT);
    }
}
```

Output:

```
{
    "a": {
        "b": {
            "c1": "a",
            "c2": 2,
            "c3": 3,
            "c4": [
                1,
                2,
                3
            ],
            "c5": [
                {
                    "d1": "09"
                },
                {
                    "d1": "08"
                },
                {
                    "d1": "07"
                }
            ]
        }
    }
}

```

### Path Expression

[](#path-expression)

- a.b selects "value":

```
    [
        "a" => [
            "b" => "value"
        ]
    ];
```

- a.2.b selects "two":

```
    [
        "a" => [
            [ "b" => "zero" ],
            [ "b" => "one" ],
            [ "b" => "two" ],
            [ "b" => "three" ]
        ]
    ];
```

- a.\*.b selects all values "zero, "one", "two" and "three":

```
    [
        "a" => [
            [ "b" => "zero" ],
            [ "b" => "one" ],
            [ "b" => "two" ],
            [ "b" => "three" ]
        ]
    ];
```

### Rule Expression

[](#rule-expression)

Expression: cast\_type|option\_1:param\_1,param\_2,...param\_n;option\_2:param\_1...;option\_n...

Where cast\_type is the identifier of cast rule, options are aditional configurations do be executed against the value, for example, trim, pad, etc.

Cast Types:

- int: casts to a int value.
- float: casts to a float point value.
- bool: casts to a boolean value.
- string: casts to a string value.
    - max\_length:size truncate the value at max length.
    - rpad:size,char pad at right position with supplied char.
    - lpad:size,char pad at left position with supplied char.

Extending
---------

[](#extending)

It is possible to add new cast rules, for this it will be necessary create a class the extends `CastRule` interface and register it with `PHP\Cast\CastRule\CastRuleFactory::registerCastRule` method. You can use the `PHP\Cast\CastRule\CastRuleBase` to shorten the path. See example above:

```
class BooleanCastRule extends PHP\Cast\CastRule\CastRuleBase {
    public function getIdentifier() {
        return "bool";
    }

    public function cast($value) {
        return (boolean) $value;
    }
}

...

PHP\Cast\CastRule\CastRuleFactory::registerCastRule(new BooleanCastRule());

...
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community6

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

Total

7

Last Release

3254d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8dbd353fcd4bad780ace25faf2294d032cf09387a9cf65fd50cd54d0a9562aec?d=identicon)[tarcisiojr](/maintainers/tarcisiojr)

---

Top Contributors

[![tarcisio-softbox](https://avatars.githubusercontent.com/u/30092158?v=4)](https://github.com/tarcisio-softbox "tarcisio-softbox (18 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/softboxlab-php-array-utils/health.svg)

```
[![Health](https://phpackages.com/badges/softboxlab-php-array-utils/health.svg)](https://phpackages.com/packages/softboxlab-php-array-utils)
```

PHPackages © 2026

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