English 中文(简体)
实验室功能,从阵列+目标中产生nes
原标题:php function that create nested ul li from array+object

i 这里有阵列。

Array
(
    [0] => stdClass Object
        (
            [tid] => 1
            [parent_id] => 0
            [language] => th
            [t_type] => category
            [t_name] => cat1
            [t_description] => 
            [t_uri] => cat1
            [t_uri_encoded] => cat1
            [t_uris] => 
            [meta_title] => 
            [meta_description] => 
            [meta_keywords] => 
            [childs] => Array
                (
                    [0] => stdClass Object
                        (
                            [tid] => 3
                            [parent_id] => 1
                            [language] => th
                            [t_type] => category
                            [t_name] => cat1.1
                            [t_description] => 
                            [t_uri] => cat1.1
                            [t_uri_encoded] => cat1.1
                            [t_uris] => 
                            [meta_title] => 
                            [meta_description] => 
                            [meta_keywords] => 
                            [childs] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [tid] => 5
                                            [parent_id] => 3
                                            [language] => th
                                            [t_type] => category
                                            [t_name] => cat1.1.1
                                            [t_description] => 
                                            [t_uri] => cat1.1.1
                                            [t_uri_encoded] => cat1.1.1
                                            [t_uris] => 
                                            [meta_title] => 
                                            [meta_description] => 
                                            [meta_keywords] => 
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [tid] => 4
                            [parent_id] => 1
                            [language] => th
                            [t_type] => category
                            [t_name] => cat1.2
                            [t_description] => 
                            [t_uri] => cat1.2
                            [t_uri_encoded] => cat1.2
                            [t_uris] => 
                            [meta_title] => 
                            [meta_description] => 
                            [meta_keywords] => 
                        )

                )

        )

    [1] => stdClass Object
        (
            [tid] => 2
            [parent_id] => 0
            [language] => th
            [t_type] => category
            [t_name] => cat2
            [t_description] => 
            [t_uri] => cat2
            [t_uri_encoded] => cat2
            [t_uris] => 
            [meta_title] => 
            [meta_description] => 
            [meta_keywords] => 
        )

)

在json的扼杀中,这种阵容+object

[{"tid":"1","parent_id":"0","language":"th","t_type":"category","t_name":"cat1","t_description":null,"t_uri":"cat1","t_uri_encoded":"cat1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"3","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.1","t_description":null,"t_uri":"cat1.1","t_uri_encoded":"cat1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"5","parent_id":"3","language":"th","t_type":"category","t_name":"cat1.1.1","t_description":null,"t_uri":"cat1.1.1","t_uri_encoded":"cat1.1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"4","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.2","t_description":null,"t_uri":"cat1.2","t_uri_encoded":"cat1.2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"2","parent_id":"0","language":"th","t_type":"category","t_name":"cat2","t_description":null,"t_uri":"cat2","t_uri_encoded":"cat2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]

how to establish nested ul li as this.

<ul>
  <li>cat1
    <ul>
        <li>cat1.1
            <ul>
                <li>cat1.1.1</li>
            </ul>
        </li>
        <li>cat1.2</li>
    </ul>
  </li>
  <li>cat2</li>
</ul>

这是我所尝试的。

echo list_nested_cat( $list_item );
function list_nested_cat( $list_item, $start = 0) {
    echo  <ul style="margin-left: 1em;"> ;
    foreach ( $list_item as $item ) {
        if ( isset( $item->childs ) && is_array( $item->childs ) ) {
            echo  <li> .$item->t_name. </li> ;
            list_nested_cat($item->childs);
        } else {
            echo  <li> .$item->t_name. </li> ;
        }

    }
    echo  </ul> ;
}

但是,这一职能重复了错误的“html”推崇的利格式。

页: 1 i 产生Arnaud576875法典中的阵列物体

PHP 类型和次类的树种结构,但不填表

最佳回答

这应当使你开始:

It uses a recursive function; it calls itself.

<>UPDATED

与JSON物体测试,并固定了一些错误

<?php    

echo make_ulli($object); 

function make_ulli($array){
    if(!is_array($array)) return   ;

    $output =  <ul> ;
    foreach($array as $item){  

        $output .=  <li>  . $item->t_name;      

        if(property_exists($item,  childs ))
            $output .= make_ulli($item->childs);

        $output .=  </li> ;

    }   
    $output .=  </ul> ;

    return $output;
}


?>
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签