从多维数组输出正确的值



我有一个多维数组"$pages_tree",其中存储了以下值:

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
                        )
                )
        )
)

我需要的是能够从这个数组中输出一个树状结构。

我已经编写了一个从数组输出的代码,看起来像这样:

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

我这样调用函数:

flatten_array($pages_tree);

我得到的输出是:

    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

但我真正想要的是:

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

任何帮助都是值得的,谢谢。。

编辑:

出于测试目的,您可以使用它来创建数组$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));

这是完整的代码,它运行得很好,根据需要进行调整;)

<?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);
?>

相关内容

  • 没有找到相关文章

最新更新