Question

I have a multidimensional array "$pages_tree" with these values stored:

Array
(
    [0] => Array
        (
            [page_id] => 300
            [page_parent] => 0
            [page_name] => MAIN 1
            [depth_level] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [page_id] => 302
                            [page_parent] => 300
                            [page_name] => SUB 1
                            [depth_level] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [page_id] => 305
                                            [page_parent] => 302
                                            [page_name] => SUB-SUB 1
                                            [depth_level] => 3
                                        )

                                    [1] => Array
                                        (
                                            [page_id] => 303
                                            [page_parent] => 302
                                            [page_name] => SUB-SUB 2
                                            [depth_level] => 3
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [page_id] => 301
            [page_parent] => 0
            [page_name] => MAIN 2
            [depth_level] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [page_id] => 304
                            [page_parent] => 301
                            [page_name] => SUB 2
                            [depth_level] => 2
                        )

                )

        )

)

What i need is to be able to output a tree like structure from this array.

I have written a code to output from the array and looks like this:

function flatten_array($array){
    echo '<ul>';

    foreach($array as $val)
    {
        if(is_array($val))
        {
            flatten_array($val);
        }
        else
        {
            echo '<li>'.$val.'</li>';
        }
    }
    echo '</ul>';
}

I call the function like this:

flatten_array($pages_tree);

The output i get is this:

    300
    0
    MAIN 1
    1
            302
            300
            SUB 1
            2
                    305
                    302
                    SUB-SUB 1
                    3
                    303
                    302
                    SUB-SUB 2
                    3
    301
    0
    MAIN 2
    1
            304
            301
            SUB 2
            2

But what i really want is this:

    MAIN 1 - 300

            SUB 1 - 302

                    SUB-SUB 1 - 305

                    SUB-SUB 2 - 303
    MAIN 2 - 301

            SUB 2 - 304

Any help is much apreciated, thanks..

EDITED:

For testing purposes, you can use this to create the array $pages_tree:

$pages_tree = array();

$pages_tree[0]['page_id'] = 300;
$pages_tree[0]['page_parent'] = 0;
$pages_tree[0]['page_name'] = 'MAIN 1';
$pages_tree[0]['depth_level'] = 1;
$pages_tree[0]['children'] = array(array("page_id"=>302, "page_parent"=>300, "page_name"=>'SUB 1', "depth_level"=>2, "children" => array(array("page_id"=>305, "page_parent"=>302, "page_name"=>'SUB-SUB 1', "depth_level"=>3), array("page_id"=>303, "page_parent"=>302, "page_name"=>'SUB-SUB 2', "depth_level"=>3))));


$pages_tree[1]['page_id'] = 301;
$pages_tree[1]['page_parent'] = 0;
$pages_tree[1]['page_name'] = 'MAIN 2';
$pages_tree[1]['depth_level'] = 1;
$pages_tree[1]['children'] = array(array("page_id"=>304, "page_parent"=>301, "page_name"=>'SUB 2', "depth_level"=>2));
Was it helpful?

Solution

This the complet code, it's working just fine, adjust is as you need ;)

<?php
$pages_tree = array();

$pages_tree[0]['page_id'] = 300;
$pages_tree[0]['page_parent'] = 0;
$pages_tree[0]['page_name'] = 'MAIN 1';
$pages_tree[0]['depth_level'] = 1;
$pages_tree[0]['children'] = array(array("page_id"=>302, "page_parent"=>300, "page_name"=>'SUB 1', "depth_level"=>2, "children" => array(array("page_id"=>305, "page_parent"=>302, "page_name"=>'SUB-SUB 1', "depth_level"=>3), array("page_id"=>303, "page_parent"=>302, "page_name"=>'SUB-SUB 2', "depth_level"=>3))));


$pages_tree[1]['page_id'] = 301;
$pages_tree[1]['page_parent'] = 0;
$pages_tree[1]['page_name'] = 'MAIN 2';
$pages_tree[1]['depth_level'] = 1;
$pages_tree[1]['children'] = array(array("page_id"=>304, "page_parent"=>301, "page_name"=>'SUB 2', "depth_level"=>2));

echo '<pre>';
print_r($pages_tree);
echo '</pre>';
//////////////////////////////////////////////////////////////////////////
function flatten_array($array){
    echo '<ul>';

    $str = ''; //Appeneded string
    $displayed = false; //Boolean show if the string is already print it or not

    //Loop
    foreach($array as $key => $val)
    {
        if(is_array($val))
        {
            if($str != ''){
                echo '<il>' . $str . '</li>'; //Display the String after
                $displayed = true; //Make the boolean true cuz the sring is already print it
            }
            //Send the child Array to the function again
            flatten_array($val);
        }
        else
        {   
            //Verify if the key if a page_id or page_name , so here you can add what you need (maybe depth_level or page_parent)
            if($key == 'page_id') $str .= $val;
            if($key == 'page_name') $str = $val . ' - ' . $str;
        }
    }
    //Print the str if it not already display it
    if($str != '' && $displayed != true) echo '<il>' . $str . '</li>';
    echo '</ul>';
}
//////////////////////////////////////////////////////////////////////////////////
flatten_array($pages_tree);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top