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

RecursiveTreeIterator

The RecursiveTreeIterator allows iterating over a RecursiveIterator to generate an ASCII graphic tree. This is a convenient and native way to display a tree-like structure.

<?php

    $data = [
        'fruit' => [
            'apple',
            'banana'
        ],
        'vegetables' => [
            'carrot',
            'broccoli',
            'leafy' => [
                'spinach',
                'lettuce'
            ]
        ]
    ];
    
    // Wrap the array in a RecursiveArrayIterator
    $iterator = new RecursiveArrayIterator($data);
    
    // Create a RecursiveTreeIterator
    $treeIterator = new RecursiveTreeIterator(
        $iterator,
        RecursiveTreeIterator::SELF_FIRST
    );
    
    // Loop and print the tree
    foreach ($treeIterator as $key => $value) {
        echo $treeIterator->getPrefix() . "$key: $value\n";
    }
    
    /**
    |-- fruit
    |   |-- 0: apple
    |   |-- 1: banana
    |-- vegetables
    |   |-- 0: carrot
    |   |-- 1: broccoli
    |   |-- leafy
    |       |-- 0: spinach
    |       |-- 1: lettuce
    */

?>

Documentation

See Also