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

  • Home
  • SEARCH
  • 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 217943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:42:17+00:00 2026-05-11T18:42:17+00:00

I need to transform my nested sets structure (mysql) into json for this spacetree

  • 0

I need to transform my nested sets structure (mysql) into json for this spacetree
1) http://blog.thejit.org/wp-content/jit-1.0a/examples/spacetree.html

I found this function to create an array from nested sets:
2) http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php

I can also convert php array into json with PHP function json_encode

My problem: the function nestify (from second link) gives me not exactly that i need. I need something like this: http://pastebin.com/m68752352

Can you help me change the function “nestify” so it gives me the correct array?

Here is this function one more time:

function nestify( $arrs, $depth_key = 'depth' )
    {
        $nested = array();
        $depths = array();

        foreach( $arrs as $key => $arr ) {
            if( $arr[$depth_key] == 0 ) {
                $nested[$key] = $arr;
                $depths[$arr[$depth_key] + 1] = $key;
            }
            else {
                $parent =& $nested;
                for( $i = 1; $i <= ( $arr[$depth_key] ); $i++ ) {
                    $parent =& $parent[$depths[$i]];
                }

                $parent[$key] = $arr;
                $depths[$arr[$depth_key] + 1] = $key;
            }
        }

        return $nested;
    }
  • 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-11T18:42:17+00:00Added an answer on May 11, 2026 at 6:42 pm

    The following snippet should do the trick, adapted from some PHP Doctrine code I found on the web :

    function toHierarchy($collection)
    {
            // Trees mapped
            $trees = array();
            $l = 0;
    
            if (count($collection) > 0) {
                    // Node Stack. Used to help building the hierarchy
                    $stack = array();
    
                    foreach ($collection as $node) {
                            $item = $node;
                            $item['children'] = array();
    
                            // Number of stack items
                            $l = count($stack);
    
                            // Check if we're dealing with different levels
                            while($l > 0 && $stack[$l - 1]['depth'] >= $item['depth']) {
                                    array_pop($stack);
                                    $l--;
                            }
    
                            // Stack is empty (we are inspecting the root)
                            if ($l == 0) {
                                    // Assigning the root node
                                    $i = count($trees);
                                    $trees[$i] = $item;
                                    $stack[] = & $trees[$i];
                            } else {
                                    // Add node to parent
                                    $i = count($stack[$l - 1]['children']);
                                    $stack[$l - 1]['children'][$i] = $item;
                                    $stack[] = & $stack[$l - 1]['children'][$i];
                            }
                    }
            }
    
            return $trees;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's almost unheard of to not use an IDE for… May 12, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer The first problem I see is that the method recursive… May 12, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer In spite of the historical interchanging of these two terms,… May 12, 2026 at 8:29 pm

Related Questions

I would like to pass a nested JavaScript object to my ASP.NET MVC Action
I deal with a lot of hierarchies in my day to day development. File
My goal is to build an engine that takes the latest HL7 3.0 CDA
I need to transform a lot of XML files (Fedora export) into a different

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.