PHPackages                             marc/flattree - 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. marc/flattree

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

marc/flattree
=============

Adjacent list tree builder

431

Since Feb 23Compare

[ Source](https://github.com/marcioAlmada/flattree)[ Packagist](https://packagist.org/packages/marc/flattree)[ RSS](/packages/marc-flattree/feed)WikiDiscussions Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

 [![](https://camo.githubusercontent.com/a075dcfbcec181a741fcbe470bf9d6a15a847b5702c7a090089983caecb2b792/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f34393534393533302f666c6174747265652f666c6154547265652e706e67 "Flat Tree")](https://camo.githubusercontent.com/a075dcfbcec181a741fcbe470bf9d6a15a847b5702c7a090089983caecb2b792/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f34393534393533302f666c6174747265652f666c6154547265652e706e67)
======================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#----)

[![Build Status](https://camo.githubusercontent.com/4a3d9850043e81514bde00fcfbd7016967291467ae297d37c26e57a6e414cc6b/68747470733a2f2f7472617669732d63692e6f72672f6d617263696f416c6d6164612f666c6174747265652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/marcioAlmada/flattree)[![Coverage Status](https://camo.githubusercontent.com/5a42bb5c78a7ea21ff08a12e3d6cb2b3d4925cac12d65352aa50638d9ccd9af7/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d617263696f416c6d6164612f666c6174747265652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/marcioAlmada/flattree?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/61941f44428ddca513d65367447fc18e2d2cb77b5dd8cf243aa71eeeedb921b1/68747470733a2f2f706f7365722e707567782e6f72672f6d6172632f666c6174747265652f762f737461626c652e706e67)](https://packagist.org/packages/marc/flattree)[![License](https://camo.githubusercontent.com/77f394e2201d7e1f20455e8d7c7d48fd17708b166bd137cab6bdb9a6eec0e20a/68747470733a2f2f706f7365722e707567782e6f72672f6d6172632f666c6174747265652f6c6963656e73652e706e67)](https://packagist.org/packages/marc/flattree)

Set Up
------

[](#set-up)

```
composer require marc/flattree:dev-master
```

Usage
-----

[](#usage)

### Recursive Adjacent List

[](#recursive-adjacent-list)

Consider you have the following adjacent list as data representing some recursive tree:

employee\_idparent\_idjob\_titlefirst\_name1NULL'Managing Director''Bill'21'Customer Services''Angela'31'Development Manager''Ben'42'Assistant 1''Henry'52'Assistant 2''Nicola'63'Snr Developer''Kerry'73'Assistant''James'86'Jrn Developer''Tim'To build a tree based on the given dataset, where `parent_id=employee_id`:

```
use marc\flatrree\{unfold, debug};

$tree = unfold($associative_data, 'parent_id=employee_id');

echo debug($tree, "{job_title}: {first_name}");
```

Debug output:

```
├─ :
│  ├─ Managing Director: Bill
│  │  ├─ Customer Services: Angela
│  │  │  └─ Assistant 1: Henry
│  │  │  └─ Assistant 2: Nicola
│  │  ├─ Development Manager: Ben
│  │  │  ├─ Snr Developer: Kerry
│  │  │  │  └─ Jrn Developer: Tim
│  │  │  └─ Assistant: James

```

### Non Recursive Adjacent List

[](#non-recursive-adjacent-list)

idclassanimalbreedsize1'mammal''dog''Dalmatian''big'2'mammal''dog''Bulldog''small'3'mammal''dog''Lhasa Apso''small'4'mammal''cat''Persian''small'Build a tree grouping by `class` and `animal`:

```
use marc\flattree\{unfold, debug};

$tree = unfold($data, ['class', 'animal']);

echo debug($tree, ['{:level}', '{:level}', '{breed}']);
```

Debug output:

```
├─ mammal
│  ├─ dog
│  │  └─ Dalmatian
│  │  └─ Bulldog
│  │  └─ Lhasa Apso
│  ├─ cat
│  │  └─ Persian

```

One more level of grouping, now by `class` and `animal` and `size`:

```
use marc\flattree\{unfold, debug};

$tree = unfold($associative_data, ['class', 'animal', 'size']);

echo debug($tree, ['{:level}', '{:level}', '{:level}', '{breed}']);
```

Debug output:

```
├─ mammal
│  ├─ dog
│  │  ├─ big
│  │  │  └─ Dalmatian
│  │  ├─ small
│  │  │  └─ Bulldog
│  │  │  └─ Lhasa Apso
│  ├─ cat
│  │  ├─ small
│  │  │  └─ Persian

```

Final Notes
-----------

[](#final-notes)

Notice `marc\flattree\debug` function is there for debug purposes *only*, you must not depend on its output.

Reference

Copyright
---------

[](#copyright)

Copyright (c) 2016-\* Márcio Almada. Distributed under the terms of an MIT-style license. See LICENSE for details.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

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

### Embed Badge

![Health badge](/badges/marc-flattree/health.svg)

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

PHPackages © 2026

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