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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:36:23+00:00 2026-05-19T03:36:23+00:00

Let’s say I have a table called my_table that looks like this: id |

  • 0

Let’s say I have a table called my_table that looks like this:

id      |  name                    | parent_id
1       |  Row 1                   | NULL
2       |  Row 2                   | NULL
3       |  Row 3                   | 1
4       |  Row 4                   | 1
5       |  Row 5                   | NULL
6       |  Row 6                   | NULL
7       |  Row 7                   | 8
8       |  Row 8                   | NULL
9       |  Row 9                   | 4
10      |  Row 10                  | 4

Basically I want my final array in PHP to look like this:

Array
(
    [0] => Array
        (
            [name] => Row 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Row 3
                            [children] => 
                        )

                    [1] => Array
                        (
                            [name] => Row 4
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Row 9
                                            [children] => 
                                        )

                                    [1] => Array
                                        (
                                            [name] => Row 10
                                            [children] => 
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => Row 2
            [children] => 
        )

    [2] => Array
        (
            [name] => Row 5
            [children] => 
        )

    [3] => Array
        (
            [name] => Row 6
            [children] => 
        )

    [4] => Array
        (
            [name] => Row 8
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Row 7
                            [children] => 
                        )

                )

        )

)

So, I want it to get all of the rows where parent_id is null, then find all nested children recursively.

Now here’s the part that I’m having trouble with:

How can this be done with 1 call to the database?

I’m sure I could do it with a simple select statement and then have PHP make the array look like this but I’m hoping this can be done with some kind of fancy db joining or something like that.

  • 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-05-19T03:36:23+00:00Added an answer on May 19, 2026 at 3:36 am

    I’m not aware of any way to get such an array from a single database call. A mysql SQL query returns a table-like dataset which is always column based. Therefore, the answer is you can’t.

    However, it is possible to make quite smart tree based queries. A very interesting read about this can be found here.

    On other systems than mysql, Common table expressions could achieve what you want.

    But still no such array out of the box.

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

Sidebar

Related Questions

No related questions found

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.