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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:13:53+00:00 2026-05-12T20:13:53+00:00

I have an interesting problem. The basis of the problem is that my last

  • 0

I have an interesting problem. The basis of the problem is that my last iteration of an array reference doesn’t
seem to “stick,” if you will. A little context: I’ve devised a very simple data structure for page heirarchy that
looks like this:

,1,2,3>,4>,5,6,7<<,8

Translation: forget about the annoying leading commas. Pages 1, 2, 3, & 8 are top-level page id’s, 4 is a subpage of 3 (the ‘>’ means to move a level deeper), and 5, 6, & 7, are subpages of 4.

A more human-readable format would look like this:

1
2
3
— 4
— — 5
— — 6
— — 7
8

Don’t ask me why I’m doing it this way. I haven’t yet come up with a simpler way of generating the structure with javascript and posting via a web form.

The problem is that everything goes great throughout the recursive function, but I lose page #8 back in my caller function. I suspect I’m mistaken on some element of recursion, variable references, and variable scope, and this has turned into quite the puzzle.

Expected Output (working just fine within the last call of the function):

Array
(
[1] => Array
    (
    )

[2] => Array
    (
    )

[3] => Array
    (
        [4] => Array
            (
                [5] => Array
                    (
                    )

                [6] => Array
                    (
                    )

                [7] => Array
                    (
                    )

            )

    )

[8] => Array
    (
    )

)

Actual Output (outside the loop):

Array
(
[1] => Array
    (
    )

[2] => Array
    (
    )

[3] => Array
    (
        [4] => Array
            (
                [5] => Array
                    (
                    )

                [6] => Array
                    (
                    )

                [7] => Array
                    (
                    )

            )

    )

)

Any thoughts?

[EDIT]: I removed a couple of residual self:: references…

CODE:

<?php
// recursive string in this format: (,\d+)*[>|<]?
//   ,      = leading comma
//   n,n+1  = comma-delimited list of page_ids
//   >      = indicates the next step in our depth-first approach
//   <      = indicates we're done with that set of children. back it up.
function parse_page_orders($page_orders, &$cur_page, &$trail)
{
    // #1 matches our comma-led, comma-delimited list of page id's
    // #2 matches our next step--forward or backward
    preg_match('/([,\d+]*)([>|<])?/', $page_orders, $matches);

    // remove this section of the page_orders variable so we can get on with our lives
    $page_orders = str_replace($matches[0], '', $page_orders);

    // #1: get the list of page ids and add it to the current page item
    $p = explode(',', $matches[1]);
    // start at 1 to skip the empty element at the beginning
    for ($i=1; $i<count($p); $i++)
    {
        $cur_page[$p[$i]] = array();
    }
    // #2: determine our next step
    if (isset($matches[2]))
    {
        if ($matches[2] == '>')
        {
            $trail[] = &$cur_page;
            parse_page_orders($page_orders, $cur_page[end($p)], $trail);
        }
        elseif ($matches[2] == '<' && count($trail)>0)
        {
            parse_page_orders($page_orders, array_pop($trail), $trail);
        }
    }
    else
    {
        // we're done. this should be our result.
        print_r($cur_page); 
    }
}
$pages = array();
$trail = array();
$page_orders = ',1,2,3>,4>,5,6,7<<,8';
parse_page_orders($page_orders, $pages, $trail);
print_r($pages);

?>
  • 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-12T20:13:54+00:00Added an answer on May 12, 2026 at 8:13 pm

    in case you’re interested how to parse the string in “your” format:

        class Parser {
    
            function run($str) {
                preg_match_all('~(\d+)|[<>]~', $str, $a);
                $this->a = $a[0];
                return $this->expr();
            }
    
            function expr() {
                $q = array();
                while(1) {
                    if(!count($this->a)) return $q;
                    $sym = array_shift($this->a);
                    if($sym == '<') return $q;
                    if($sym == '>')
                        $q[count($q) - 1]['children'] = $this->expr();
                    else
                        $q[] = array('id' => $sym);
                }
            }
        }
    
    
        $a = "1,2,3>4,>5,6,7<<,8>9,10,>11<,12,<,13,14";
        $p = new Parser;
        $result = $p->run($a);
        print_r($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem that i can't seem to figure out. I am
We have an interesting problem with WCF binding and streaming transfer mode that we
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have an interesting problem here I've been trying to solve for the last
I have an interesting problem. I currently have a basic template library that renders
I have an interesting problem to solve here that may require some creative direction.
I have a interesting problem in Java, its a little wordy though so bear
Here's an interesting problem. I have an ETL script written in c# that I
I have an interesting problem that I need help with. I am currently working
I have an interesting problem that I've been trying to simplify. I have a

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.