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

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

danielcosta/tree
================

A simple helper package to allow using Trees on PHP

1.0.0(8y ago)0105MITPHP

Since Apr 5Pushed 7y ago1 watchersCompare

[ Source](https://github.com/danielcosta/php-helper-tree)[ Packagist](https://packagist.org/packages/danielcosta/tree)[ RSS](/packages/danielcosta-tree/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

PHP Helper - Tree
=================

[](#php-helper---tree)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8f9e5d97f8219d60dd2eaa82555cba24683840a1f959f235b72d822e21d2db74/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e69656c636f7374612f747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danielcosta/tree)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/8c162866e753e72e8a9daaa536e6f4101cd7507f850d326c83337033320704fd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f64616e69656c636f7374612f7068702d68656c7065722d747265652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/danielcosta/php-helper-tree)[![Coverage Status](https://camo.githubusercontent.com/4530c2ce431108ce47a6dcfdf19886017ce93ae9954bf2ace424d2c6a8fc41c9/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f64616e69656c636f7374612f7068702d68656c7065722d747265652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/danielcosta/php-helper-tree/code-structure)[![Quality Score](https://camo.githubusercontent.com/63c7ea608fc2b901b4bbbee7a628fb8771183b0a0721c89b6284322498965321/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f64616e69656c636f7374612f7068702d68656c7065722d747265652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/danielcosta/php-helper-tree)[![Total Downloads](https://camo.githubusercontent.com/8934ebfa182318345d2d4dd0c163329aa38152bc3d689a86a463ea9cf9d2d006/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e69656c636f7374612f747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danielcosta/tree)

Quick Installation
------------------

[](#quick-installation)

```
composer require danielcosta/tree
```

Usage
-----

[](#usage)

To use, just get an array like bellow

```
$flatArray = [
    [
        'id' => 1,
        'name' => 'Level 1',
        'parentId' => 0,
        'nodes' => [],
    ],
    [
        'id' => 3,
        'name' => 'Level 3',
        'parentId' => 2,
        'nodes' => [],
    ],
    [
        'id' => 2,
        'name' => 'Level 2',
        'parentId' => 1,
        'nodes' => [],
    ],
];
```

And pass it to the helper `Tree`, using `Tree::makeFromFlatArray($flatArray);`. The result will be exactly this:

```
$result = Tree::makeFromFlatArray($flatArray);
print_r($result);
/*
Array
(
    [id] => 1
    [name] => Level 1
    [parentId] => 0
    [nodes] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [name] => Level 2
                    [parentId] => 1
                    [nodes] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [name] => Level 3
                                    [parentId] => 2
                                    [nodes] => Array
                                        (
                                        )
                                )
                        )
                )
        )
)
*/
```

Optional parameters to this method are:

- $parentId = 0 - when you want to set the first level of the return
- string $key = 'id' - the primary key on your flat array
- string $parentKey = 'parentId' - the parent key on your flat array
- string $childKey = 'nodes' - where to put the child nodes

Buy me a coffee
---------------

[](#buy-me-a-coffee)

[![paypal](https://camo.githubusercontent.com/e1ff554a09e8e92bef25abc553ff05b88f45afd695877cf12f3a46558ef65b2e/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e61746543435f4c472e676966)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4A6GYRWAGVMNG)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

3009d ago

### Community

Maintainers

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

---

Top Contributors

[![danielcosta](https://avatars.githubusercontent.com/u/42549?v=4)](https://github.com/danielcosta "danielcosta (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mixdinternet/cities

Pacote com os estados, cidades e bairros do Brasil

231.4k](/packages/mixdinternet-cities)[jackbayliss/livewire-select2

Simple to use Livewire component for Select2

153.0k](/packages/jackbayliss-livewire-select2)

PHPackages © 2026

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