3

i have array+object like this.

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

)

this array+object in json string

[{"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 create nested ul li like 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>

this is what i have tried

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>';
}

but this function echo incorrect html nested ul li format.

ps. i create array object from the code of arnaud576875

PHP tree structure for categories and sub categories without looping a query

3
  • Please post that ugly array as a JSON string for testing purposes? Commented Apr 25, 2012 at 3:51
  • @iambriansreed i posted JSON string as you request. Commented Apr 25, 2012 at 9:14
  • @vee I used it to test my code and fixed some errors. See the updated code posted. Commented Apr 25, 2012 at 15:40

1 Answer 1

3

This should get you started:

It uses a recursive function; it calls itself.

UPDATED

Tested with the JSON object and fixed some errors

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


?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.