Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:26:17+00:00 2026-06-18T15:26:17+00:00

I’m having difficulties with the following scenario, I have an array with categories, so

  • 0

I’m having difficulties with the following scenario, I have an array with categories, so categories can have child categories and the child categories can have child categories infinitely. Now what I’m trying to achieve is the following, but I just can’t manage to achive this.

I have an array $items in the following structure

Array
(
    [0] => Array
        (
            [label] => Main Cat
            [id] => 29
            [parent_id] => 19
        )

    [1] => Array
        (
            [label] => Main Cat
            [id] => 17
            [parent_id] => 19
        )

    [2] => Array
        (
            [label] => Main Cat
            [id] => 20
            [parent_id] => 19
            [items] => Array
                (
                    [0] => Array
                        (
                            [label] => Child Level 1
                            [id] => 21
                            [parent_id] => 20
                        )

                    [1] => Array
                        (
                            [label] => Child Level 1
                            [id] => 22
                            [parent_id] => 20
                            [items] => Array
                                (
                                    [0] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 27
                                            [parent_id] => 22
                                            [items] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [label] => Child Level 3
                                                            [id] => 28
                                                            [parent_id] => 27
                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [label] => Child Level 1
                            [id] => 23
                            [parent_id] => 20
                        )

                    [3] => Array
                        (
                            [label] => Child Level 1
                            [id] => 24
                            [parent_id] => 20
                            [items] => Array
                                (
                                    [0] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 25
                                            [parent_id] => 24
                                        )

                                    [1] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 26
                                            [parent_id] => 24
                                        )

                                )

                        )

                )

        )

)

Now I want the following array to display in a table with each child level indented, and if it should go one level up from Child Level 3 to Child Level 2 the indetation should reverse again.

Example

<table border="2" width="100%">
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
    <tr>
        <td style="margin-left:60px;">Child Level 3</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
</table>

My PHP code looks like this thus far,

public function renderCategoriesRecursive($items)
        {
                foreach($items as $item)
                {

                        $itemCount = count($item['items']);



                        echo CHtml::openTag('tr');
                            echo CHtml::openTag('td',array('class'=>$class));
                                echo $item['label'];
                            echo CHtml::closeTag('tr');
                        echo CHtml::closeTag('tr');


                        if(isset($item['items']) && $itemCount)
                        {

                                $this->renderCategoriesRecursive($item['items']);

                        }
                }
}

The variable $items contains the array above

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:26:18+00:00Added an answer on June 18, 2026 at 3:26 pm

    I noticed that your own solution referenced the variable $class, so I’ve incorporated that too. You really don’t need to use CHtml (unless you do, but I’ve never seen such a case)

    function renderItems( $array, $class, $indent = 0  ) {
    
        foreach( $items as $item) {
            echo '<tr><td class="'.$class.'">'.$item['label'].'</td></tr>';
    
            if( isset( $item['items'] && count( $item['items'] )
                renderItems($item['items'],$class,$indent+1);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I upgraded downgraded to rails 2.3.17 due to the security bugs, but now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.