Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Afficher un arbre nativement

By Alexandre Daubois

<?php

$data = [
    'src' => [
        'Controller' => ['HomeController.php', 'ApiController.php'],
        'Entity' => ['User.php', 'Product.php'],
        'Kernel.php',
    ],
    'tests' => ['AppTest.php'],
    'composer.json',
];

$tree = new RecursiveTreeIterator(
    new RecursiveArrayIterator($data)
);

foreach ($tree as $line) {
    echo $line . "\n";
}

// Output:
// |-src
// | |-Controller
// | | |-HomeController.php
// | | \-ApiController.php
// | |-Entity
// | | |-User.php
// | | \-Product.php
// | \-Kernel.php
// |-tests
// | \-AppTest.php
// \-composer.json

// (also works with RecursiveDirectoryIterator!)

PHP dispose d’un rendu d’arbre ASCII intégré. Dans la SPL.

RecursiveTreeIterator prend n’importe quelle structure récursive (tableaux, répertoires, XML…) et produit un arbre indenté formaté.

Le comportement de la commande tree. En PHP. Depuis la version 5.3.

Je suis certain que je l’aurais utilisé plusieurs fois si j’avais su qu’il existait.

See Also

PHP Features

Last updated: 11 October 2026