多维数组.....嵌套下拉列表?

时间:2013-02-25 05:23:58

标签: php multidimensional-array

 Array
(
[0] => Array
    (
        [id] => 1
        [parent] => 0
        [title] => Parent 1
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [parent] => 1
                        [title] => Child 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 3
                                        [parent] => 2
                                        [title] => child 2
                                        [children] => Array
                                            (
                                            )

                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 9
                        [parent] => 1
                        [title] => parent of one
                        [children] => Array
                            (
                            )

                    )

                [2] => Array
                    (
                        [id] => 19
                        [parent] => 1
                        [title] => df
                        [children] => Array
                            (
                            )

                    )

            )

    )

[1] => Array
    (
        [id] => 4
        [parent] => 0
        [title] => parent 2
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 5
                        [parent] => 4
                        [title] => child 4
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 6
                                        [parent] => 5
                                        [title] => child 5
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 7
                                                        [parent] => 6
                                                        [title] => child 7
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                                [1] => Array
                                                    (
                                                        [id] => 8
                                                        [parent] => 6
                                                        [title] => child 8
                                                        [children] => Array
                                                            (
                                                                [0] => Array
                                                                    (
                                                                        [id] => 12
                                                                        [parent] => 8
                                                                        [title] => child 9
                                                                        [children] => Array
                                                                            (
                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                                [1] => Array
                                    (
                                        [id] => 17
                                        [parent] => 5
                                        [title] => child unknown
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 18
                                                        [parent] => 17
                                                        [title] => asdasd
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

[2] => Array
    (
        [id] => 13
        [parent] => 0
        [title] => parent 3
        [children] => Array
            (
            )

    )

[3] => Array
    (
        [id] => 14
        [parent] => 0
        [title] => parent 4
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 21
                        [parent] => 14
                        [title] => sad
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 22
                                        [parent] => 21
                                        [title] => sdfsaf
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 23
                                                        [parent] => 22
                                                        [title] => test
                                                        [children] => Array
                                                            (
                                                                [0] => Array
                                                                    (
                                                                        [id] => 24
                                                                        [parent] => 23
                                                                        [title] => tester
                                                                        [children] => Array
                                                                            (
                                                                                [0] => Array
                                                                                    (
                                                                                        [id] => 25
                                                                                        [parent] => 24
                                                                                        [title] => tested
                                                                                        [children] => Array
                                                                                            (
                                                                                                [0] => Array
                                                                                                    (
                                                                                                        [id] => 26
                                                                                                        [parent] => 25
                                                                                                        [title] => example
                                                                                                        [children] => Array
                                                                                                            (
                                                                                                                [0] => Array
                                                                                                                    (
                                                                                                                        [id] => 27
                                                                                                                        [parent] => 26
                                                                                                                        [title] => examples 
                                                                                                                        [children] => Array
                                                                                                                            (
                                                                                                                            )

                                                                                                                    )

                                                                                                            )

                                                                                                    )

                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

[4] => Array
    (
        [id] => 15
        [parent] => 0
        [title] => parent 5
        [children] => Array
            (
            )

    )

[5] => Array
    (
        [id] => 16
        [parent] => 0
        [title] => parent 6
        [children] => Array
            (
            )

    )

[6] => Array
    (
        [id] => 20
        [parent] => 0
        [title] => aaa
        [children] => Array
            (
            )

    )

)

有关如何从此数组中生成嵌套下拉列表的任何建议吗?

我不知道从哪里开始......

1 个答案:

答案 0 :(得分:0)

$HOST="localhost";
$DB="db_dir";
$USER="root";
$PASS="";

mysql_connect($HOST,$USER,$PASS);
mysql_select_db($DB);

function RecursiveCat($pid)
{
static $level=0;
static $strid="";
static $strname="";

$sql=mysql_query("select * from categories where cat_parent =".$pid." ");
//var_dump($sql);

while($row=mysql_fetch_assoc($sql))
{
    $id=$row['cat_id'];
    $level--;
    $pad="";

    for($p=1;$p<($level*-1);$p++) $pad.="&nbsp;&nbsp;&nbsp;> ";

    $strname.='<option value="'.$row['cat_id'].'">'.$pad.$row['cat_title'].'</option>';

    $rid=RecursiveCat($id);
    $strid[]=$row['cat_id'];
    $level++;
}

return $strname;
}

<?php echo '<select name="parent">'.'<option value="0">None</option>';
echo RecursiveCat(0);echo '</select>';?>