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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:40:31+00:00 2026-05-31T20:40:31+00:00

I am trying to convert following table data into nested array using PHP. I

  • 0

I am trying to convert following table data into nested array using PHP. I am almost done but stuck at one point.

id  name                    parent
1   Apparel                                                                   
2   Appliances                                                                
46  Apparel                 1                                                 
47  Child Apparel           46                                                
49  Child Apparel 2         47        

Using this code

$cats = array(); // from database

$refs = array();
$rcats = array();

foreach ($cats as $cat) {
    $thisref = &$refs[$cat['id']];

    $thisref['id'] = $cat['id'];
    $thisref['name'] = $cat['name'];
    $thisref['leaf'] = "true";

    if (!$cat['parent']) {
        $rcats[$cat['id']] = &$thisref;
    } else {
        unset($refs[$cat['parent']]['leaf']);
        $refs[$cat['parent']]['items'][$cat['id']] = &$thisref;
    }
}          

print_r(json_encode($rcats));                             

This results into following JSON.

{
    "1": {
        "id": "1",
        "name": "Apparel",
        "items": {
            "46": {
                "id": "46",
                "name": "Apparel",
                "items": {
                    "47": {
                        "id": "47",
                        "name": "Child Apparel",
                        "items": {
                            "49": {
                                "id": "49",
                                "name": "Child Apparel 2",
                                "leaf": "true"
                            }
                        }
                    }
                }
            }
        }
    },
    "2": {
        "id": "2",
        "name": "Appliances",
        "leaf": "true"
    }
}

Where as I want the JSON like

[
    {
        "id": "1",
        "name": "Apparel",
        "items": [
            {
                "id": "46",
                "name": "Apparel",
                "items": [
                    {
                        "id": "47",
                        "name": "Child Apparel",
                        "items": [
                            {
                                "id": "49",
                                "name": "Child Apparel 2",
                                "leaf": "true"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": "2",
        "name": "Appliances",
        "leaf": "true"
    }
]
  • 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-31T20:40:33+00:00Added an answer on May 31, 2026 at 8:40 pm

    Here I’m assuming you are storing your final results in $refs

    print_r(json_encode(array_values($rcats)));
    

    If you want to take out all the indexes, in your for loop, do [] instead of the id:

    if (!$cat['parent']) {
        $rcats[] = &$thisref;
    } else {
        unset($refs[$cat['parent']]['leaf']);
        $refs[$cat['parent']]['items'][] = &$thisref;
    }
    

    A quick example I throw:

    <?php
    
    $a = array('1' => array('a' => 3), '2' => array('b' => 4));
    echo json_encode($a) . "\n";
    echo json_encode(array_values($a)) . "\n";
    

    Outputs:

    {"1":{"a":3},"2":{"b":4}}
    [{"a":3},{"b":4}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert a php/mysql generated table into a downloadable csv file.
I am trying to convert the following table into something the is a bit
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm trying to convert the following jquery to javascript syntax, but being not familiar
I am trying to convert the following SQL into a LINQ expression SELECT COUNT(ID)
I'm trying to convert the following list into a select list so it can
Im trying to copy over data from one table to another in the same
I am trying to convert the following stored proc to a LinqToSql call (this
I am trying to convert the following two interfaces from a C header file
I m trying to convert the following code to another AJAX call, in order

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.