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

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

bluestone/tree
==============

Build tree data structure

v1.0.2(3y ago)04.6k↓33.3%MITPHPPHP ^8.1

Since Nov 23Pushed 3y ago2 watchersCompare

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

READMEChangelogDependencies (3)Versions (4)Used By (0)

About Tree
==========

[](#about-tree)

[![Run tests](https://github.com/bluestonelab/tree/actions/workflows/run_tests.yml/badge.svg)](https://github.com/bluestonelab/tree/actions/workflows/run_tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/23f0b1d9a9067743f6aa444ebd68d2cfcb2385e244c65ddeb35313c17dab8d88/68747470733a2f2f706f7365722e707567782e6f72672f626c756573746f6e652f747265652f762f737461626c65)](https://packagist.org/packages/bluestone/tree)

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

[](#installation)

This package requires `php:^8.1`.
You can install it via composer:

```
composer require bluestone/tree
```

Usage
-----

[](#usage)

Tree instance need items to forge the expected data structure.

### How to forge a tree with array as items ?

[](#how-to-forge-a-tree-with-array-as-items-)

By default, a tree expect `id` and `parentId` fields in give items like this :

```
use Bluestone\Tree\Tree;

$items = [
    ['id' => 1, 'parentId' => null, 'username' => 'Robert'],
    ['id' => 2, 'parentId' => 1, 'username' => 'John'],
    ['id' => 3, 'parentId' => 1, 'username' => 'Jane'],
];

$tree = new Tree($items);

// Get Robert's children
$robert = $tree->getNodeById(1); // Return the node representing Robert
$robert->username; // Robert
$robert->getChildren(); // Return an array containing John & Jane

// Get Jane's parent
$jane = $tree->getNodeById(3);
$jane->getParent(); // Return Robert's node
```

### How to forge a tree with custom fields ?

[](#how-to-forge-a-tree-with-custom-fields-)

You can customize the `parentKey` and the `primaryKey` like this :

```
use Bluestone\Tree\Tree;

$items = [
    [
        'uuid' => 'b08f82b9-e8cc-44fb-b99a-4929bfcf02a4',
        'parentUuid' => null,
        'username' => 'Robert',
    ],
    [
        'uuid' => 'eaaf3215-17ba-4779-b444-d4a8203f1096',
        'parentUuid' => 'b08f82b9-e8cc-44fb-b99a-4929bfcf02a4',
        'username' => 'John',
    ],
];

$tree = new Tree($items, 'parentUuid', 'uuid');
```

### How to serialize a tree to json ?

[](#how-to-serialize-a-tree-to-json-)

```
use Bluestone\Tree\Tree;

$items = [
    ['id' => 1, 'parentId' => null, 'name' => 'Chicken'],
    ['id' => 2, 'parentId' => 1, 'name' => 'Egg'],
];

$tree = new Tree($items);

$json = json_encode($tree);
```

The result of the `json_encode` is a string with contains :

```
{
    "primaryKey": "id",
    "parentKey": "parentId",
    "nodes": [
        {"id": 1, "parentId": null, "name": "Chicken"},
        {"id": 2, "parentId": 1, "name": "Egg"}
    ]
}
```

### How to forge a tree with object as items ?

[](#how-to-forge-a-tree-with-object-as-items-)

You can pass object as items with expected `id` and `parentId` public property like this :

```
use Tests\Artifacts\Person;
use Bluestone\Tree\Tree;

$items = [
    new Person(1, null, 'Chicken'),
    new Person(2, 1, 'Egg'),
];

$tree = new Tree($items);

$chicken = $tree->getNodeById(1);
$chicken->name; // Chicken
$chicken->getChildren(); // Return Egg's node
```

Contributing
------------

[](#contributing)

Tree is an open source project under MIT License and is open for contributions.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.8% 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 ~1 days

Total

3

Last Release

1270d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d65323c79234220d55e60e176f917e1c1db9b90215702e3e11f8b692977285b8?d=identicon)[Willi@m](/maintainers/Willi@m)

---

Top Contributors

[![william-suppo](https://avatars.githubusercontent.com/u/6459452?v=4)](https://github.com/william-suppo "william-suppo (7 commits)")[![actions-user](https://avatars.githubusercontent.com/u/65916846?v=4)](https://github.com/actions-user "actions-user (2 commits)")

---

Tags

datatree

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[fakerphp/faker

Faker is a PHP library that generates fake data for you.

3.9k358.5M3.5k](/packages/fakerphp-faker)[dflydev/dot-access-data

Given a deep data structure, access data by dot notation.

718359.1M86](/packages/dflydev-dot-access-data)[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)[mbezhanov/faker-provider-collection

A collection of custom providers for the Faker library

2138.6M24](/packages/mbezhanov-faker-provider-collection)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3123.4M20](/packages/php-units-of-measure-php-units-of-measure)

PHPackages © 2026

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