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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:43:16+00:00 2026-06-15T23:43:16+00:00

I use PHP and mySQL with Idiorm . That might not be relevant. My

  • 0

I use PHP and mySQL with Idiorm. That might not be relevant.

My PHP array

  • It’s a relationship between parents and childs.
  • 0 is the root parent.
  • Example: Root parent 0 have the child 33 which have the child 27 which have
    the child 71.

This array structure can be changed if needed for solving the problem.

array (
  33 => 
    array (
      0 => '27',
      1 => '41',
  ),
  27 => 
    array (
      0 => '64',
      1 => '71',
  ),
  0 => 
    array (
      0 => '28',
      1 => '29',
      2 => '33',
  ),
)

My hierarchical result

Something like this, but as an array…

  0 => 
      28
      29
      33
         27 =>
               64
               71
         41

Information

  • The depth are unkown and it can be unlimited. I tried foreach, but it might not be the way.

My own thoughts

  • Some recursive function?
  • Some while loops?

I tried both of the above, just got a mess. It’s a brainer.

  • 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-15T23:43:17+00:00Added an answer on June 15, 2026 at 11:43 pm

    The suggestion by @deceze worked. However the input array needs to change a litte, like this…

    $rows = array(
        array(
            'id' => 33,
            'parent_id' => 0,
        ),
        array(
            'id' => 34,
            'parent_id' => 0,
        ),
        array(
            'id' => 27,
            'parent_id' => 33,
        ),
        array(
            'id' => 17,
            'parent_id' => 27,
        ),
    );
    

    From https://stackoverflow.com/a/8587437/476:

    function buildTree(array $elements, $parentId = 0) {
        $branch = array();
    
        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $children = buildTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
    
        return $branch;
    }
    
    $tree = buildTree($rows);
    
    print_r( $tree );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on web application that will use PHP & MySQL. Application will
Are there any pre-made scripts that I can use for PHP / MySQL to
Is there any good rating and voting script tutorials that use PHP and MySQL?
Use php and MySQL, I want to get the duration in php code between
I am trying to use PHP and MySQL's Create Table Select between two different
I use Idiorm as ORM for MySQL with PHP. I need to check if
Use PHP and Mysql. Fields in my table (product) are id , full_price ,
I don't use php or mysql but I am trying to upgrade a phpbb
I use php to display a table of data drawn from my mysql database.
I am trying to use a PHP form (new.php) to update 2 MySQL tables

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.