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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:41:09+00:00 2026-05-13T08:41:09+00:00

I am attempting a basic recursion to create multi-dimensional arrays based on the values

  • 0

I am attempting a basic recursion to create multi-dimensional arrays based on the values of an inputed array.

The recursion works by checking for a value we shall call it “recursion” to start the loop and looks for another value we’ll call it “stop_recursion” to end.

Basically taking this array

array('One', 'Two', 'recursion', 'Three', 'Four', 'Five', 'stop_recursion', 'Six', 'Seven')

And making this array

array('One', 'Two', array('Three', 'Four', 'Five'), 'Six', 'Seven')

The code I have for it so far is as follows

function testRecrusion($array, $child = false)
{
    $return = array();

    foreach ($array as $key => $value) {
        if ($value == 'recursion') {
            unset($array[$key]);
            $new = testRecrusion($array, true);
            $array = $new['array'];
            $return[] = $new['return'];
        } else {
            if ($value == 'stop_recursion') {
                unset($array[$key]);
                if ($child) {
                    return array('return' => $return, 'array' => $array);
                }
            } else {
                unset($array[$key]);
                $return[] = $value;
            }
        }
    }

    return $return;
}

But the output from that is

Array
(
    [0] => One
    [1] => Two
    [2] => Array
        (
            [0] => Three
            [1] => Four
            [2] => Five
        )

    [3] => Three
    [4] => Four
    [5] => Five
    [6] => Six
    [7] => Seven
)

I guess the real question is…will an array values continuously loop through the first values given from the initial call or once the new array is returned and set will it loop through that new array. I know the answer is basically right here saying that yes it will continue the old array value, but shouldn’t this work vice-versa?

Any help will be appreciated 🙂

———— edit ——————-

I might as well add that while I can perform this action using a much simpler method, this needs to be recursively checked since this will be ported to a string parser that could have a infinite number of child arrays.

  • 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-13T08:41:09+00:00Added an answer on May 13, 2026 at 8:41 am

    When you return inside the recursion, you need to return both the inner array and the index from which to continue searching for elements so that you don’t look at the same element twice. Try this instead:

    function testRecursionImpl($array, $i)
    {
        $return = array();
    
        for (; $i < sizeof($array); ++$i) {
            if ($array[$i] == 'recursion') {
                $new = testRecursionImpl($array, $i + 1);
                $return[] = $new[0];
                $i = $new[1];
            } else if ($array[$i] == 'stop_recursion') {
                return array($return, $i);
            } else {
                $return[] = $array[$i];
            }
        }
    
        return array($return, $i);
    }
    
    function testRecursion($array)
    {
        $result = testRecursionImpl($array, 0);
        return $result[0];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create a basic Hangman program. It's an array of tags
I am attempting to load some images into a very basic C++/SDL based program.
I have a basic Tree built in Flex. The Tree works fine. I'm attempting
I am attempting to create a very basic login screen, using the Storyboard designer
I am attempting to compile a basic array wrapper template class into a .o
I'm attempting to create an iOS 5 app with some very basic FTP functionality
I am attempting to create a simple java form using Swing. The idea basic
I am attempting to create something along the lines of How to Implement Basic
I am attempting to create a custom messaging app. Just something basic but I
I am currently attempting to create a fairly basic web app that has the

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.