PHPackages                             smoren/tree-tools - 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. smoren/tree-tools

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

smoren/tree-tools
=================

Iterator-based tree-tools

v1.0.0(3y ago)7148MITPHPPHP &gt;=7.4.0

Since Jan 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Smoren/tree-tools-php)[ Packagist](https://packagist.org/packages/smoren/tree-tools)[ RSS](/packages/smoren-tree-tools/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

PHP Tree Tools
==============

[](#php-tree-tools)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/7dc37be9432422b8f99fddb32bdbe680ec966916ab004dbd688d09b591e593ff/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f747265652d746f6f6c73)](https://camo.githubusercontent.com/7dc37be9432422b8f99fddb32bdbe680ec966916ab004dbd688d09b591e593ff/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f747265652d746f6f6c73)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7b969e3ccbb294f8f05bffb127546af2cceb1c61ed43fa146ef6c3748c644b6f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f536d6f72656e2f747265652d746f6f6c732d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Smoren/tree-tools-php/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/a58c8b319dd7067c152d5efd17642c2a005664b4c046dd041b7567a4a0ae6c40/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f536d6f72656e2f747265652d746f6f6c732d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Smoren/tree-tools-php?branch=master)[![Build and test](https://github.com/Smoren/tree-tools-php/actions/workflows/test_master.yml/badge.svg)](https://github.com/Smoren/tree-tools-php/actions/workflows/test_master.yml/badge.svg)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Library for working with trees.

How to install to your project
------------------------------

[](#how-to-install-to-your-project)

```
composer require smoren/tree-tools

```

Quick reference
---------------

[](#quick-reference)

#### Tree Walker

[](#tree-walker)

ReducerDescriptionCode Snippet[`traverseDepthFirst`](#Traverse-Depth-First)Iterates a tree using depth-first search`TreeWalker::traverseDepthFirst($data, $childrenContainerKey)`[`traverseBreadthFirst`](#Traverse-Breadth-First)Iterates a tree using breadth-first search`TreeWalker::traverseBreadthFirst($data, $childrenContainerKey)`#### Tree Builder

[](#tree-builder)

ReducerDescriptionCode Snippet[`build`](#Build)Builds a tree from given flat collection`TreeBuilder::build($collection, $idField, $parentIdField, $childrenContainerField, $itemContainerField)`Usage
-----

[](#usage)

### Tree Walker

[](#tree-walker-1)

#### Traverse Depth First

[](#traverse-depth-first)

Iterates a tree like a flat collection using depth-first traversal.

`TreeWalker::traverseDepthFirst(iterable $data, ?string $childrenContainerKey = null): Generator`

If `$childrenContainerKey` is not null looks for children items using by this key only.

Otherwise, considers any subarray to contain children.

```
use Smoren\TreeTools\TreeWalker;

$tree = [
    [
        'id' => 1,
        'children' => [
            ['id' => 11],
            [
                'id' => 12,
                'children' => [
                    ['id' => 121],
                    ['id' => 122],
                ],
            ],
        ],
    ],
    [
        'id' => 2,
        'children' => [
            ['id' => 21],
        ],
    ],
    ['id' => 3],
];

$result = [];

foreach(TreeWalker::traverseDepthFirst($tree) as $item) {
    $result[] = $item['id'];
}
var_dump($result);
// [1, 11, 12, 121, 122, 2, 21, 3]
```

#### Traverse Breadth First

[](#traverse-breadth-first)

Iterates a tree like a flat collection using depth-breadth traversal.

`TreeWalker::traverseBreadthFirst(iterable $data, ?string $childrenContainerKey = null): Generator`

If `$childrenContainerKey` is not null looks for children items using by this key only.

Otherwise, considers any subarray to contain children.

```
use Smoren\TreeTools\TreeWalker;

$tree = [
    [
        'id' => 1,
        'children' => [
            ['id' => 11],
            [
                'id' => 12,
                'children' => [
                    ['id' => 121],
                    ['id' => 122],
                ],
            ],
        ],
    ],
    [
        'id' => 2,
        'children' => [
            ['id' => 21],
        ],
    ],
    ['id' => 3],
];

$result = [];

foreach(TreeWalker::traverseBreadthFirst($tree) as $item) {
    $result[] = $item['id'];
}
var_dump($result);
// [1, 2, 3, 11, 12, 21, 121, 122]
```

### Tree Builder

[](#tree-builder-1)

#### Build

[](#build)

Builds a tree from given flat collection of items with relations.

```
TreeBuilder::build(
    iterable $collection,
    string $idField = 'id',
    string $parentIdField = 'parent_id',
    string $childrenContainerField = 'children',
    string $itemContainerField = 'item'
): array

```

```
use Smoren\TreeTools\TreeBuilder;

$input = [
    ['id' => 1, 'name' => 'Item 1', 'parent_id' => null],
    ['id' => 2, 'name' => 'Item 1.1', 'parent_id' => 1],
    ['id' => 3, 'name' => 'Item 1.2', 'parent_id' => 1],
    ['id' => 4, 'name' => 'Item 1.1.1', 'parent_id' => 2],
    ['id' => 5, 'name' => 'Item 2', 'parent_id' => null],
    ['id' => 6, 'name' => 'Item 3', 'parent_id' => null],
    ['id' => 7, 'name' => 'Item 3.1', 'parent_id' => 6],
    ['id' => 8, 'name' => 'Item 3.2', 'parent_id' => 6],
];

$tree = TreeBuilder::build($input);
print_r($tree);
/*
[
    [
        'id' => 1,
        'name' => 'Item 1',
        'parent_id' => null,
        'children' => [
            [
                'id' => 2,
                'name' => 'Item 1.1',
                'parent_id' => 1,
                'children' => [
                    [
                        'id' => 4,
                        'name' => 'Item 1.1.1',
                        'parent_id' => 2,
                        'children' => [],
                    ]
                ],
            ],
            [
                'id' => 3,
                'name' => 'Item 1.2',
                'parent_id' => 1,
                'children' => [],
            ],
        ],
    ],
    [
        'id' => 5,
        'name' => 'Item 2',
        'parent_id' => null,
        'children' => [],
    ],
    [
        'id' => 6,
        'name' => 'Item 3',
        'parent_id' => null,
        'children' => [
            [
                'id' => 7,
                'name' => 'Item 3.1',
                'parent_id' => 6,
                'children' => [],
            ],
            [
                'id' => 8,
                'name' => 'Item 3.2',
                'parent_id' => 6,
                'children' => [],
            ],
        ]
    ],
]
*/
```

Unit testing
------------

[](#unit-testing)

```
composer install
composer test-init
composer test

```

License
-------

[](#license)

Tree Tools PHP is licensed under the MIT License.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

2

Last Release

1222d ago

Major Versions

v0.1.0 → v1.0.02023-01-11

### Community

Maintainers

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

---

Top Contributors

[![Smoren](https://avatars.githubusercontent.com/u/7403235?v=4)](https://github.com/Smoren "Smoren (9 commits)")

---

Tags

phptreetree-buildertree-structuretree-walkergeneratoriteratortreetraverse

###  Code Quality

TestsCodeception

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smoren-tree-tools/health.svg)

```
[![Health](https://phpackages.com/badges/smoren-tree-tools/health.svg)](https://phpackages.com/packages/smoren-tree-tools)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[nikic/iter

Iteration primitives using generators

1.1k5.9M38](/packages/nikic-iter)[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k27.6M92](/packages/simplesoftwareio-simple-qrcode)[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)[loophp/collection

A (memory) friendly, easy, lazy and modular collection class.

745663.8k13](/packages/loophp-collection)

PHPackages © 2026

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