PHPackages                             rgilyov/array-tree-walker - 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. rgilyov/array-tree-walker

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

rgilyov/array-tree-walker
=========================

Very easy way to parse arrays with tree structure

1.0.0(8y ago)15.4k1MITPHPPHP &gt;=5.6

Since Aug 2Pushed 8y ago1 watchersCompare

[ Source](https://github.com/RomaGilyov/ArrayTreeWalker)[ Packagist](https://packagist.org/packages/rgilyov/array-tree-walker)[ RSS](/packages/rgilyov-array-tree-walker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

ArrayTreeWalker
===============

[](#arraytreewalker)

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

[](#installation)

```
composer require rgilyov/array-tree-walker dev-master
```

Description
-----------

[](#description)

Lets make an example of array with tree structure:

```
$tree = [
    'name'    => 'child',
    'parents' => [
        'mother' => [
            'name'    => 'mother',
            'parents' => [
                'mother' => [
                    'name' => 'first grand mother'
                ],
                'father' => [
                    'name' => 'first grand father'
                ]
            ]
        ],
        'father' => [
            'name'    => 'father',
            'parents' => [
                'mother' => [
                    'name' => 'second grand mother'
                ],
                'father' => [
                    'name'    => 'second grand father',
                    'parents' => [
                        'mother' => [
                            'name' => 'second grand grand mother'
                        ]
                    ]
                ]
            ]
        ]
    ]
];

$walker = new \RGilyov\ArrayTreeWalker($tree);
```

To walk through tree structure use `->`, to get value use `->get('name')`;

```
echo $walker->parents->mother->parents->father->get('name'); // result: `first grand father`
```

You can also get value like so

```
echo $walker->parents->mother->parents->father['name']; // result: `first grand father`

echo $walker->parents->mother->parents->father->name['name']; // result: `first grand father`

echo $walker->parents->mother->parents->father->name->father['name']; // result: null
```

You may also specify node name, just pass it as the second the class's constructor parameter

```
$walkerWithNodeName = new \RGilyov\ArrayTreeWalker($tree, 'parents');

echo $walkerWithNodeName->father->father->get('name'); // result: `second grand father`

echo $walkerWithNodeName->father->father->mother['name']; // result: 'second grand grand mother'

echo $walkerWithNodeName->father->father->mother->mother['name']; // result: null
```

You also can walk down the tree and walk up the tree and modify each node during walking:

```
$walker = new \RGilyov\ArrayTreeWalker($tree, 'parents');

/*
 * If an array will be returned the values for given keys will be rewritten
   and if keys didn't exist in the node they will be attached
 */
$walker->walkDown(function ($node, $level, $nodeNumber, $parentNodeNumber) {

    return [
        'level' => $level,
        'node_number' => $nodeNumber,
        'parent_node_number' => $parentNodeNumber
    ];

});

$walker->walkUp(function ($node, $nodeChildren, $parameters, $level, $nodeNumber) {

    return [
        'super_name' => $node['name'] . ' ' . implode(array_map(function ($child) {
            return $child['name'];
        }, $nodeChildren), ' ')
    ];

});
```

List of available methods:

```
$walker->toArray();
$walker->count();
$walker->offsetExists('key');
$walker->offsetUnset('key');
$walker->offsetGet('key');
$walker->offsetSet('key', 'value');
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

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

Unknown

Total

1

Last Release

3208d ago

### Community

Maintainers

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

---

Top Contributors

[![RomaGilyov](https://avatars.githubusercontent.com/u/14930432?v=4)](https://github.com/RomaGilyov "RomaGilyov (12 commits)")

---

Tags

treeparsenested array

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rgilyov-array-tree-walker/health.svg)

```
[![Health](https://phpackages.com/badges/rgilyov-array-tree-walker/health.svg)](https://phpackages.com/packages/rgilyov-array-tree-walker)
```

###  Alternatives

[knplabs/knp-menu

An object oriented menu library

1.4k55.8M287](/packages/knplabs-knp-menu)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[enqueue/dsn

Parse DSN

12525.5M45](/packages/enqueue-dsn)[gabrielelana/byte-units

Library to parse, format and convert byte units

1672.2M19](/packages/gabrielelana-byte-units)[bluem/tree

Library for handling tree structures based on parent IDs

252916.1k7](/packages/bluem-tree)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

320392.1k17](/packages/codewithdennis-filament-select-tree)

PHPackages © 2026

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