Solved

Recursive Arrays

Posted on 2007-03-17
2
526 Views
Last Modified: 2013-12-13
Can anyone make a simple recursive function that puts arrays inside arrays?

The goal of the function is to make an array of arrays:

For example the function:

recurse(7);

should produce:

$tree = array(array(array(array(array(array(array()))))));

I am going to use this for conforming with the Tree data structure.
0
Comment
Question by:pagemastah
2 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 18740295
function recurseArray($depth) {
  if($depth <=1) {
     return array();
  }
  return array(recurseArray($depth -1));
}

-r-
0
 
LVL 18

Expert Comment

by:JoseParrot
ID: 18742850
Pay attention to the amount of required memory.
If the array are of Integers (4 bytes each) and each array size is 10 then you need 4 x 10 power 7 bytes. Say, 38MB. But if its size is 100, then you need something like 380 Terabytes.

Jose
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question