my code stock.com

Indentation d'un tableau par niveau

Snippet options

Download: Download snippet as drawtree.php.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

// Fonction de dessin basique d'un "array" PHP sous forme de treeview
// 10.05.2011 - [email protected] - http://www.memepasmal.ch/2011/05/10/php-array-treeview/
function drawTree($myarray, $level = 0)
{

    // Boucle sur chaque élément du tableau
    foreach($myarray as $key => $value)
    {
   
        // En cas d'objet on convertit en tableau
        if (is_object($value)) $value = (array)$value;
       
        // Si l'élément est un tableau
        if (is_array($value)) {
       
            // On l'affiche en tant que noeud
            echo '<div style="padding-left: ' . ($level * 20) . 'px">[' . $key . ']</div>';
           
            // Puis on affiche son arborescence, à un niveau supérieur
            drawTree($value, $level + 1);
           
        } else {
       
            // C'est une valeur, on l'affiche
            echo '<div style="padding-left: ' . ($level * 20) . 'px">' . $key . ' = <b>' . $value . '</b></div>';
           
        }
       
    }

}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.