PHPackages                             pbergman/tree-helper - 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. [CLI &amp; Console](/categories/cli)
4. /
5. pbergman/tree-helper

ActiveLibrary[CLI &amp; Console](/categories/cli)

pbergman/tree-helper
====================

A symfony console helper to print a tree

1.0.2(4y ago)24469.4k—6.8%5[1 issues](https://github.com/pbergman/tree-printer/issues)4MITPHPPHP &gt;=5.4.0

Since Mar 26Pushed 3y ago1 watchersCompare

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

READMEChangelog (3)Dependencies (1)Versions (4)Used By (4)

Tree builder helper
===================

[](#tree-builder-helper)

this is a small library for symfony console to print a tree in a fluent and flexible way.

The output is similar as the linux tree command, an can be used to debug/output relational data.

### Usage:

[](#usage)

```
$tree = new TreeHelper($output);

$tree
    ->newNode('first')
        ->addValue('fist value')
        ->addValue('second value')
        ->newNode('fist sub')
            ->addValue('fist sub value')
            ->addValue('second sub value')
            ->newNode('fist sub sub')
                ->addValue('fist sub sub value')
                ->addValue('second sub sub value')
            ->end()
        ->end()
    ->newNode('second')
        ->addValue('second value')
        ->addValue('second second value')
    ->end();

$tree->printTree();

```

or

```
$tree = new TreeHelper($output);
$tree->addArray([
    'first' => [
        0 => 'fist value',
        1 => 'second value',
        'fist sub' => [
            0 => 'fist sub value',
            1 => 'second sub value',
            'fist sub sub' => [
                'fist sub sub value',
                'second sub sub value',
            ],
        ],
    ],
    'second' => [
        'second value',
        'second second value',
    ]
]);

```

that will output:

```
.
│
└── first
    ├── fist value
    ├── second value
    ├── fist sub
    │   ├── fist sub value
    │   ├── second sub value
    │   └── fist sub sub
    │       ├── fist sub sub value
    │       └── second sub sub value
    └── second
        ├── second value
        └── second second value

```

for other example see the the tests.

### Methods

[](#methods)

#### newNode($name)

[](#newnodename)

Create an new node and bind it to the parent

#### addNode($nodeobject:self)

[](#addnodenodeobjectself)

Append node to tree

#### end()

[](#end)

Will return the parent object (if set else null)

#### getRoot()

[](#getroot)

Will return the root node

#### printTree($outputobject:OutputInterface)

[](#printtreeoutputobjectoutputinterface)

Print the tree to output

#### findNode($name)

[](#findnodename)

Search a node by title

#### toArray()

[](#toarray)

Returns a array representation of the data

#### getTitle()

[](#gettitle)

Returns the title of the node

#### setTitle($title)

[](#settitletitle)

Sets the title of the node

#### addValue($value)

[](#addvaluevalue)

Add a value to the node, this can be a valid scalar or a object with method \_\_toString

#### addArray($array)

[](#addarrayarray)

Add a nested array to node

#### setValues($array)

[](#setvaluesarray)

Add a flat array to node, this be set as the values of the node

#### setParent($parentobject:self)

[](#setparentparentobjectself)

Set the parent of the node, will trow a RuntimeException is the the node all ready is linked to the given node, so we don`t get infinite loops.

#### getNodes()

[](#getnodes)

Get all the (child) nodes defined in the node, all the nodes are only saved in the root so to get all nodes you probably have to do this $t-&gt;getRoot()-&gt;getNodes(); else it will return the nodes where the parent is set as node where from you calling the method.

#### setMaxDepth($depth)

[](#setmaxdepthdepth)

Set the max depth of the node values, this can be done globally (on root) or on a specific node.

#### getMaxDepth()

[](#getmaxdepth)

Return the max depth that is set

#### setFormats()

[](#setformats)

Set the form that is used for example:

```
$tree->setFormats([
    TreeHelper::LINE_PREFIX_EMPTY => '  ',
    TreeHelper::LINE_PREFIX => '│ ',
    TreeHelper::TEXT_PREFIX => '├ ',
    TreeHelper::TEXT_PREFIX_END => '└ ',
]);

```

no it will use a 2 space indent instead of 4, see tests

#### setFormat($id, $format)

[](#setformatid-format)

Overwrite a specific format

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 63.6% 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 ~1255 days

Total

3

Last Release

1560d ago

### Community

Maintainers

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

---

Top Contributors

[![pbergman](https://avatars.githubusercontent.com/u/1193198?v=4)](https://github.com/pbergman "pbergman (14 commits)")[![jmdz](https://avatars.githubusercontent.com/u/1752126?v=4)](https://github.com/jmdz "jmdz (5 commits)")[![Quezler](https://avatars.githubusercontent.com/u/3179271?v=4)](https://github.com/Quezler "Quezler (2 commits)")[![colinodell](https://avatars.githubusercontent.com/u/202034?v=4)](https://github.com/colinodell "colinodell (1 commits)")

---

Tags

consolesymfonyarraybuildertree

### Embed Badge

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

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

###  Alternatives

[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[matthiasnoback/symfony-console-form

Use Symfony forms for Console command input

368264.8k8](/packages/matthiasnoback-symfony-console-form)[phlib/console-process

Console implementation.

1833.5k2](/packages/phlib-console-process)[eddiriarte/console-select

A fancy selection interface for symfony's console component.

117.2k2](/packages/eddiriarte-console-select)[zachleigh/artisanize

Use Laravel Artisan command syntax in any Symfony Console project.

122.6k1](/packages/zachleigh-artisanize)

PHPackages © 2026

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