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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:59:21+00:00 2026-06-18T06:59:21+00:00

Given an array: $arrData = array( 0 => array ( ‘uid’ => 1, ‘name’

  • 0

Given an array:

$arrData = array(
 0 => array (
   'uid'   => 1,
   'name'  => 'label',
   'open'  => 0,
   'close' => 9
 ),
 1 => array (
   'uid'   => 2,
   'name'  => 'label',
   'open'  => 1,
   'close' => 2
 ),
 2 => array (
   'uid'   => 3,
   'name'  => 'label',
   'open'  => 3,
   'close' => 8
 ),
 3 => array (
   'uid'   => 4,
   'name'  => 'label',
   'open'  => 4,
   'close' => 5
 ),
 4 => array (
   'uid'   => 5,
   'name'  => 'label',
   'open'  => 6,
   'close' => 7
 )
);

Which represents this structure:

<label>     [0,9]
 <label />  [1,2]
 <label>    [3,8]
  <label /> [4,5]
  <label /> [6,7]
 </label>
</label>

I am trying to get to an array that results in this format:

$arrNesting = array(
 0=>array(
   'item'    => array('uid'=>1, 'name'=>'label', 'open'=>0, 'close'=>9),
   'children' => array(
     0=>array(
       'item'     => array('uid'=>2, 'name'=>'label', 'open'=>1, 'close'=>2),
       'children' => array()
     ),
     1=>array(
       'item'     => array('uid'=>3, 'name'=>'label', 'open'=>3, 'close'=>8),
       'children' => array(
         0=>array(
           'item'     => array('uid'=>2, 'name'=>'label', 'open'=>4, 'close'=>5),
           'children' => array()
         ),
         1=>array(
           'item'     => array('uid'=>3, 'name'=>'label', 'open'=>6, 'close'=>7),
           'children' => array()
         )
       )
     )
   )
 )
);

via a function call like this:

// $arrData: Source Data with keys for denoting the left and right node values
// 'open': the key for the left node's value
// 'close': the key for the right node's value
$arrNested = format::nestedSetToArray( $arrData, 'open', 'close' );

So far, I’ve tried this format, assuming the $arrData values will always be in left value ascending order. The uid values ‘happen’ to also be in order, but are not relied upon to be so:

public static function nestedSetToArray( $arrData = array(), $openKey='open', $closeKey='close') {
// Hold the current Hierarchy
$arrSets = array();

// Last parent Index, starting from 0
$intIndex = 0;

// Last Opened and Closed Node Values, and maximum node value in this set
$intLastOpened = 0;
$intLastClosed = null;
$intMaxNodeNum = null;

// loop through $arrData
foreach( $arrData as $intKey=>$arrValues) {
  if( !isset( $arrSets[ $intIndex ] )) {
      // Create a parent if one is not set - should happen the first time through
      $arrSets[ $intIndex ] = array ('item'=>$arrValues,'children'=>array());
      $intLastOpened = $arrValues[ $openKey ];
      $intLastClosed = null; // not sure how to set this for checking 2nd IF below
      $intMaxNodeNum = $arrValues[ $closeKey ];
  } else {
      // The current item in $arrData must be a sibling or child of the last one or sibling of an ancestor
      if( $arrValues[ $openKey ] == $intLastOpened + 1) {
         // This is 1 greater then an opening node, so it's a child of it
      } else if( /* condition for sibling */ ) {
         // This is 1 greater than the intLastClosed value - so it's a sibling
      } else if( /* condition for sibling of ancestor */ ) {
         // This starts with a value greater than the parent's closing value...hmm
      }
  }
}

}

Any pointers to take this further would be appreciated.

  • 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-18T06:59:22+00:00Added an answer on June 18, 2026 at 6:59 am

    This should works

    $stack = array();
    $arraySet = array();
    
    
    foreach( $arrData as $intKey=>$arrValues) {
        $stackSize = count($stack); //how many opened tags?
        while($stackSize > 0 && $stack[$stackSize-1]['close'] < $arrValues['open']) {
                array_pop($stack); //close sibling and his childrens
                $stackSize--;
            }
    
        $link =& $arraySet;
        for($i=0;$i<$stackSize;$i++) {
            $link =& $link[$stack[$i]['index']]["children"]; //navigate to the proper children array
        }
        $tmp = array_push($link,  array ('item'=>$arrValues,'children'=>array()));
        array_push($stack, array('index' => $tmp-1, 'close' => $arrValues['close']));
    
    }
    
    return $arraySet;
    

    I skip parametrized open and close tags but you can simply add it.

    EDIT

    What is happening here:

    First the $stack is empty so we skip while().
    Then we assign reference to $arraySet to the $link, because $stack is empty we push first element to $link which is reference to $arraySet. array_push()return 1 because this is the new length of$arraySet
    Next we add an element to the
    $stackwhit values(‘index’ => 0, ‘close’ => 10)`

    Next element:
    Now $stack has 1 element, but $stack[0]['close'] is greater than $arrValues['open'] for element so we skip while.
    Again we set reference to $arraySet to $link but now there is on element in $stack so we assign $link[$stack[0]['index']]["children"] reference to $link so now $link is pointing to $arraySet[0]["children"].
    Now we push element to this children array. $tmp gives us size of this children array and we push proper element to the stack.

    Next element:
    It looks exactly like second one but at the beginning we pop one element from stack. So after this iteration on stack are two elements

    ('index' => 0, 'close' => 10)
    ('index' => 0, 'close' => 8)
    

    Next element:
    There is two element on stack but both have greater close attribute then $arrValues['open'] so we skip while loop. Then in navigation part:

    • $link is pointing to $arraySet
    • then to $arraySet[0]["children"]
    • then to $arraySet[0]["children"][0]["children"]

    And we push element to this array.
    And so on…

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

Sidebar

Related Questions

This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array like this: Array => ( [0] => 1, [1] => 2,
We gave a given array which can be in 4 states. array has values
I am trying to find an efficient way to compare a given array that
Need a function which return next N elements from given array, with given offset,
I have this given array of ids assigned to a certain variable, $post_id =
Write a program which by given array of integer values (containing negative integers) finds
I'm trying to create a bit vector set from a given array. Not sure
In order to find out which element occurs most often in a given array,
Description: Given an array consists of n positive integers, find the largest C which

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.