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 8838743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:05:09+00:00 2026-06-14T10:05:09+00:00

I am trying to turn flat mysql rows into a tree structure. Here is

  • 0

I am trying to turn flat mysql rows into a tree structure. Here is the id’s of the categories and Sub categories

Array
(
[50] => Array
(
[70] => Array
(
[0] => Array
(
[73] => Array
(
[80] => Array
(
)

                            )

                        [74] => Array
                            (
                            )

                        [75] => Array
                            (
                            )

                    )

            )

        [71] => Array
            (
                [0] => Array
                    (
                    )

            )

        [72] => Array
            (
                [0] => Array
                    (
                    )

            )

        [73] => Array
            (
                [0] => Array
                    (
                        [80] => Array
                            (
                            )

                    )

            )

        [74] => Array
            (
                [0] => Array
                    (
                    )

            )

        [75] => Array
            (
                [0] => Array
                    (
                    )

            )

        [80] => Array
            (
                [0] => Array
                    (
                    )

            )

    )

[51] => Array
    (
        [76] => Array
            (
                [0] => Array
                    (
                        [77] => Array
                            (
                            )

                    )

            )

        [77] => Array
            (
                [0] => Array
                    (
                    )

            )

        [78] => Array
            (
                [0] => Array
                    (
                        [79] => Array
                            (
                                [81] => Array
                                    (
                                    )

                            )

                    )

            )

        [79] => Array
            (
                [0] => Array
                    (
                        [81] => Array
                            (
                            )

                    )

            )

        [81] => Array
            (
                [0] => Array
                    (
                    )

            )

    )

)

I wan the output in this way

 <ul id="red" class="treeview-famfamfam">
    <li>50
        <ul> <li> 70 
                <ul><li> 73 <ul> 
                                <li> 80</li></ul>
                    </li>
                    <li> 74</li>
                    <li>75 </li>
                    </ul>
            </li>
            <li>71</li>
            <li> 72</li>
            </ul>
            </li>

BAsically i need to build a tree with categories sub categories sub sub …. categoreis etc.
Thanks in advance

  • 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-14T10:05:10+00:00Added an answer on June 14, 2026 at 10:05 am

    Edited: OK. I spent half a day on this. I’m sorry to tell you this, but you’ll have to change the definition of your array. Instead of an empty array for the leaf nodes, use a non-array value that has any value… Like this:

    $ar=array("50" => array ( "70" => array ( "0" => array("35" => "leaf"),"3"=>"leaf" ) ) );
    

    Then use this:

    function MakeTree($array){
        $return="";
    
            if(!is_array($array)) return;
            while($member=current($array)){     
    
                    $return.="<li>".key($array);
                    $return.="<ul>".MakeTree($member)."</ul></li>";
    
                next($array);           
             }       
            return $return;
        }
    
        $ar=array("50" => array ( "70" => array ( "0" => array("35" => "leaf"),"3"=>"leaf" ) ) );
        echo(MakeTree($ar));
    

    I don’t know why, but if the leaf node has null or empty array value, it doesn’t get accessed by next($array). Even if it is an empty string, so “35”=>”” won’t work.

    If you don’t use next() and current() you can’t use key(), so a change in the structure is inevitable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to turn a multidimensional array into a patterned string with this
I have a flat table that I'm trying to turn into an xml output
I am trying to turn an query result into classes. $result->setFetchMode(PDO::FETCH_CLASS, 'myclass', array()); This
Trying to turn this: href=/wp-content/themes/tray/img/celebrity_photos/photo.jpg into: href=/img/celebrity_photos/photo.jpg So I'm simply trying to remove /wp-content/themes/tray/
I'm trying to turn this XML string into a select I have @Schedule XML
I am trying to turn something I requested from a MySQL table into a
Here is the code I was trying to turn into a list comprehension: table
I'm trying to turn an array like this: 0, 1, 2, 2, 1, 0,
I'm trying to turn a huge configuration array in PHP, that looks like this
I'm trying to turn this: /index.php?param1=1&param2=1&param3=1 into this: /index.php?param1=1&param3=1 I would have thought this

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.