Help - Search - Members - Calendar
Full Version: PHP: Cycling through all levels of an array?
Invision Power Services > Community Forums > Community Web Design and Coding
_
This is something that has been baffling me for a looong time. How do you cycle through all levels of an array? For example, how to have this array:
CODE
array(
   'test' => array(
       'foo',
       'bar'
   ),
   'foobar' => array(
       'this',
       'that' => array(
           'this',
           'that'
       )
   )
)


Return this:

CODE
- test
-- foo
-- bar
- foobar
-- this
-- that
--- this
--- that


And continue working no matter how many levels are added? It's bound to be possible since IPB does it, with having unlimited subforums.

Thanks for any help.
Stephen
CODE
function recursive_array($array) {
static $indent = 0;
$indent++;

foreach ($array as $key => $var) {
if (is_array($var)) {
echo str_pad(" ", $indent + 1, "-", STR_PAD_LEFT) . $key ."<br />";
recursive_array($var);
}
else {
echo str_pad(" ", $indent + 1, "-", STR_PAD_LEFT) . $var . "<br />";
}
}
$indent--;
}


Haven't tried that but it should work, or at least give you a running start
Rikki
You need to use recursion; that is, a function that calls itself.

CODE


function doArrayThing($array, $currentprefix)
{
foreach($array as $thing)
{
if(is_array($thing))
{
doArrayThing($thing, $currentprefix . "-");
}
else
{
echo $currentprefix . $thing . "<br>";
}
}
}

doArrayThing($myarray, "-");

_
I was convinced you couldn't do recursive functions in PHP. O__O
Stephen - It doesn't quite work, just returns:

CODE
foobarthisthisthat


[edit] I got it. This is the code:

CODE
<?php

$array = array(
  'test' => array(
      'foo',
      'bar'
  ),
  'foobar' => array(
      'this',
      'that' => array(
          'this',
          'that'
      )
  )
);

function doArrayThing($array, $currentprefix)
{
foreach($array as $key => $thing)
{
if(is_array($thing))
{
echo $currentprefix . $key . "<br />";
doArrayThing($thing, $currentprefix . "-");
}
else
{
echo $currentprefix . $thing . "<br />";
}
}
}

doArrayThing($array, "");

?>
Stephen
take a look again, fixed it
_
Just going to add another question onto this topic.
I'm attempting to write something that'll generate an array like I posted in the beginning from a MySQL select. Let's say the select returns this:

CODE
array(
       '0' => array(
               'name' => 'test',
               'parent' => NULL
               ),
       '1' => array(
               'name' => 'foo',
               'parent' => '0'
               ),
       '2' => array(
               'name' => 'bar',
               'parent' => '0'
               ),
       '3' => array(
               'name' => 'foobar',
               'parent' => NULL
               ),
       '4' => array(
               'name' => 'this',
               'parent' => '3'
               ),
       '5' => array(
               'name' => 'that',
               'parent' => '3'
               ),
       '6' => array(
               'name' => 'this',
               'parent' => '5'
               ),
       '7' => array(
               'name' => 'that',
               'parent' => '5'
               )
       );


How would I write a function to get that arranged into the array I first posted?

This is really hard to me. =_=
Rikki
CODE


foreach($array as $id => $info)
{
$main_array[ $info['parent'] ][] = $info;
}



That should do it though you'd probably want to add some other stuff to it (like checking for null and acting on it).
_
I need to learn not asking questions when I'm tired. Can't believe it was that simple. laughing.gif
Thank you very much. original.gif

Actually, hmm.
That makes the key of the array item be the parent. pinch.gif Also, something else goes wrong here:
CODE
<?php
$array = array(
      '0' => array(
              'name' => 'test',
              'parent' => NULL
              ),
      '1' => array(
              'name' => 'foo',
              'parent' => '0'
              ),
      '2' => array(
              'name' => 'bar',
              'parent' => '0'
              ),
      '3' => array(
              'name' => 'foobar',
              'parent' => NULL
              ),
      '4' => array(
              'name' => 'this',
              'parent' => '3'
              ),
      '5' => array(
              'name' => 'that',
              'parent' => '3'
              ),
      '6' => array(
              'name' => 'this',
              'parent' => '5'
              ),
      '7' => array(
              'name' => 'that',
              'parent' => '5'
              )
      );

$arranged = array();

foreach($array as $key => $value)
{
       if(is_null($value['parent']))
       {
               $arranged[$value['parent']] = array();
       }
       
       $arranged[$value['parent']][$value['name']] = $value;
}

function arrange($array, $currentprefix)
{
       foreach($array as $key => $thing)
       {
               if(is_array($thing))
               {
                       echo $currentprefix . $key . "<br />";
                       arrange($thing, $currentprefix . "-");
               }
               else
               {
                       echo $currentprefix . $thing . "<br />";
               }
       }
}

arrange($arranged, "-");

?>


Returning:
CODE
-
--foobar
---foobar
---
-0
--foo
---foo
---0
--bar
---bar
---0
-3
--this
---this
---3
--that
---that
---3
-5
--this
---this
---5
--that
---that
---5


blink.gif
_
Anyone? unsure.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.