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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:10:33+00:00 2026-05-11T06:10:33+00:00

I have a set of objects in a hierachy. There’s a top root node

  • 0

I have a set of objects in a hierachy. There’s a top ‘root’ node and that has child nodes, which in turn have child nodes etc. I’m trying to save this structure into a DB using the nested set model, where each ‘side’ of each node is numbered to define the hierarchy, as in Managing Hierarchical Data in MySQL:

alt text
(source: mysql.com)

My problem is calculating the left and right values. I typically use RecursiveIteratorIterator to iterate over the hierarchy, but I cant work out how to calculate the numbers without resorting to a recursive function that parses an index variable by reference.

Any ideas?

It’s probably of no use, but this is the (incorrect) code I currently have:

$iterator = new RecursiveIteratorIterator(     new Node_List(array($root)),     RecursiveIteratorIterator::SELF_FIRST);  $i = 0;      foreach ($iterator as $node) {     $node->left = ++$i;     $node->right = ++$i; } 

As you can see, that would give something like this:

Node      Node      Node  

Left and right values of:

Node (1, 2)     Node (3, 4)     Node (5, 6) 

When they should be:

Node (1, 6)     Node (2, 3)     Node (4, 5) 
  • 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. 2026-05-11T06:10:33+00:00Added an answer on May 11, 2026 at 6:10 am

    I figured it out, here’s the solution (simplifed):

    $iterator = new RecursiveIteratorIterator(     new Site_Node_List(array($root)),     RecursiveIteratorIterator::SELF_FIRST);  $sides = array(); $s = 0; $i = 0; $parents = array(); foreach ($iterator as $item) {     $js = array_splice($parents, $depth, count($parents), array($i));     foreach (array_reverse($js) as $j) {         $sides[$j]['right'] = ++$s;     }     $sides[$i]['left'] = ++$s;     $i++; } foreach (array_reverse($parents) as $j) {     $sides[$j]['right'] = ++$s; } 

    This is am over simplified version of my actual code, as it just stores the ‘side’ values in a separate array, but it demonstrates the principle.

    The basic idea is that you store all the parent nodes (tracked by the depth value) in an array, and only write the ‘left’ values in your loop. Then, when the depth decreases it means you’ve gone back up the hierarchy, so the parents array is spliced to remove the ones that are no longer relevant, and they are looped over (in reverse) setting the ‘right’ values. Finally, you have to loop over the remaining parents at the end.

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

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • 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 ViewState is loaded before the Page_Load event. If you want… May 12, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer As far as I know you can't return value from… May 12, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer Here's the answer. The primary reason Dictionary throws is that… May 12, 2026 at 8:43 pm

Related Questions

I have a set of objects in a hierachy. There's a top root node
I have a set of configuration items I need to persist to a human
I'm working on a database design for groups hierarchy used as the foundation of
I'm writing a .NET web application in which administrators can customize the various data
I'm trying to set up an inheritance hierarchy similar to the following: abstract class

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.