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

The Archive Base Latest Questions

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

Another recursion question. What I’ve tried to develop for the past few hours is

  • 0

Another recursion question.

What I’ve tried to develop for the past few hours is a function that will let me generate a flat array of the same pages, but with the children having an identifier in front of them, (i.e. “— Sub Page, — — Sub Sub Page, etc”).

I have a hierarchical array of pages, with each child page nested inside of their parent page. Here’s an example of their output:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Parent Page
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [parent_id] => 1
                            [title] => Another Sub Page
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [title] => Sub Page
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [parent_id] => 3
                                            [title] => Sub Sub Page
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 8
            [parent_id] => 0
            [title] => Another Parent Page
        )

)

I’ve been able to get it working until I reach the second level of the recursion and then it’s still generating only one —. I want to make the solution recursive.

What I’m looking for in the final output using the example above is:

Array
(
    [1] => Parent Page
    [12] => — Another Sub Page
    [3] => — Sub Page
    [7] => — — Sub Sub Page
    [8] => Another Parent Page
)
  • 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-27T20:42:17+00:00Added an answer on May 27, 2026 at 8:42 pm
    function generate_array(array $arr, &$output = array(), $index = 0)
    {
        foreach($arr as $item)
        {
            $output[$item['id']] = str_repeat('— ', $index) . $item['title'];
            if(isset($item['children']))
            {
                generate_array($item['children'], $output, $index + 1);
            }
        }
        return $output;
    }
    
    $output = generate_array($arr);
    

    The data stored in $output would be:

    Array
    (
        [1] =>  Parent Page
        [12] => — Another Sub Page
        [3] => — Sub Page
        [7] => — — Sub Sub Page
        [8] =>  Another Parent Page
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another basic Rails question: I have a database table that needs to contain references
Another question related to this one . I have a List<SortableObjects> that is the
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
Another clipboard question: When text is put onto the clipboard, it frequently goes in
So I'm back with another ray tracing question. My code renders spheres all fine
the question is: suppose I have an input function like sin(2-cos(3*A/B)^2.5)+0.756*(C*D+3-B) specified with a
first StackOverflow question. I'm writing a predicate in prolog that takes three parameters. These
I was exploring another question , when I hit this behaviour in Sql Server

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.