Jump to content

[SOLVED] method for looping through multidimensional array of unknown depth


PC Nerd

Recommended Posts

Hi,

 

As my subject states, how can I loop through an array of unknown depth?

 

eg:

$array = array();

$array[][][][][] = "value";

 

now assuming that each level would have data.... etc - how can i proccess this data?  Im using it like building a directory tree for a menu - or for proccessing XML etc.

Im hoiping that such a method ( in theory), could be applied to most languages like Javascript (AJAX) proccessing XML of unknown depth into an array, then displaying into table etc, so on so forth .

 

Can anyone suggest a method.  All that ive come up with so far is having a foreach() mested in a foreach() etc, to a very large depth with checks to see if its an array ( iF(isArray() {foreach... if(isArray(){ foreach {}}} etc).

 

Any help is much appreciated.

Thanks

Link to comment
Share on other sites

try this:

 

$test = array('one'=>'abc','two'=>'def');

$sweet = array('test'=>$test,'a' => 'apple', 'b' => 'banana');

$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

 

function test_print($item, $key)

{

    echo "$key holds $item <br />";

}

 

array_walk_recursive($fruits, 'test_print');

 

 

output will be:

 

one holds abc

two holds cde

a holds apple

b holds banana

sour holds lemon

 

but it will be applicable to PHP5

 

http://www.php.net/manual/en/function.array-walk-recursive.php

Link to comment
Share on other sites

Ahhh thankyou - Ill look into it.

 

Also - is there a method of doing this through loops etc, that isnt language specific ( well ...)  ideally im looking for somethign that will work with JavaScript and Python, preferably not using any language specific function ( ie.... substr() sort of functiosn are usually multi-lingual, even if the arguments and name change).

 

I cannot find a Javascript one, and therefore think i need a more "cross language" method.

 

Thanks - any more suggestions?

 

* a) that works in php so thatnkyou, im alreayd using it and b) i know this isnt a JavaScript or Pythoon board, however im simply saying it as an example.  ulimately im looking for a loop solution.

Link to comment
Share on other sites

Hi,

 

As the previous poster pointed out you need to use a technique called recursion: http://en.wikipedia.org/wiki/Recursion_(computer_science).

 

Recursion is cross language, and is usually implemented as a function which calls itself. This is one of the more complex techniques in programming, and some people never understand it. Here is an example of the standard way to print the numbers 1 - 10:

 

for($i = 1; $i <= 10; $i++) {
   echo $i . PHP_EOL;
}

 

To do the same using recursion, you could do the following:

 

<?php
function printNums($num) {
  if($num < 1)
    return;
  printNums($num-1);
  echo $num . PHP_EOL;
}

printNums(10);

 

Note that the printNums function is recursive because it calls itself.

 

In terms of looping through an array of unknown depth, you would probably need a function which took an array as the argument. If the function was passed and empty array, it would return. If the array was not empty, it would call itself with the arrays child array as the argument.

 

Robin

Link to comment
Share on other sites

Howdy,

 

agreed...recursion is the way forward.

 

what do you actually want to do with the original array? just output each value, flatten it, split into mini arrays, map to two arrays with keys and values sharing keys, validate each value? is it multi-deminsional on numerical keys, or is it associative? etc. etc.

 

if you could post your original foreach( foreach( foreach code, or an example array and expected output it would really helpful.

 

Also, be warned when trying recursive functions. if you set up infinite recursion (i.e. the function never returns and constantly calls itself) then things will crash.

Link to comment
Share on other sites

that recursive setup looks like its gonna work...

as for the foreach loop:

its just somethign i threw together that loops through an array, if the value is an array, it loop shtrough that array, and so on so forth for 3 levels.  if the value is an array past this point, it simply displays "Array too large". 

 

Im gonna have a play around with recursive functions etc.  they look awesome - and yeah im aware theyll kill my server etc ( well php will timeout etc wont it?)

 

Either way - thanks, this is exactly what I wanted.

 

Topic Solved

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.