0

So as an example I have this structure with pages in a website. The thing is I have to get these items in a dropdown list, but they have to intend based on their parent.

  • loadSubItems(id) returns the subitems for the id.
  • loadSubItems(0) will give the subitems from the root, which in this case is 9, 16, 20
  • loadSubItems(9) will give an empty array loadSubItems(16) will return 17, 18, 19

The problem is that the middle loop (the foreach) has to keep on looping as long as there are subitems for that level, print them and for each level an extra intend "_" has to be added.

I have been trying to solve this for hours and I've lost my thinking and I can't seem to figure out what has to happen to be able to do this.

Any help is welcome! Thanks in advance:

Testdata below:

https://dl.dropboxusercontent.com/u/10123290/structuur.png

I'm using the following code to go to the second level:

    $items = loadSubItems(0); // returns 9 16 20
    $intend = "";
    foreach($items as $item) {
        $subitems = loadSubItems($item); 
        if(count($subitems) > 0) {
            echo $intend.$item." has subitems: <br />";
            $intend .= "_";
            foreach($subitems as $subitem) {
                echo $intend.$subitem."<br />";
            }
        } else {
            echo "<br />".$item." has no subitems <br />";
        }
    }

This code has output:

9 has no subitems 
16 has subitems: 
_17
_18
_19

20 has no subitems 

edit: So what I want as a result would be:

9 has no subitems 
16 has subitems: 
_17
__21
___22
__23
___24
_18
_19

20 has no subitems
Paul
  • 139,544
  • 27
  • 275
  • 264
Chickenlal
  • 13
  • 3
  • Most people will skim your code looking for **a question** with it's proper question marks (and maybe bold typography) and the code, so I recommend you to ask a concise question to get good answers. – Francisco Presencia Apr 27 '13 at 15:07
  • 3
    @FrankPresenciaFandos my *guess* is, he's looking for a recursive function, but it's hard to tell.. – thaJeztah Apr 27 '13 at 15:10
  • @thaJeztah I think he is, therefore I think this is a duplicate of [Deep recursive array of directory structure in PHP](http://stackoverflow.com/q/952263/938236) – Francisco Presencia Apr 27 '13 at 15:12
  • Hmm looks a bit like the link you gave me yes, I will look into it, thanks in advance! I'm indeed looking for some kind of recursive function, but after breaking my head for too long I can't see straight anymore and it's not making any sense anymore lol! – Chickenlal Apr 27 '13 at 15:15
  • @FrankPresenciaFandos or another one, like this one [Creating a recursive category tree function](http://stackoverflow.com/questions/12285694/creating-a-recursive-category-tree-function)? :) – thaJeztah Apr 27 '13 at 15:16
  • 1
    Basically you're looking at a multidimensional array. Have a look at [array_walk_recursive()](http://php.net/manual/en/function.array-walk-recursive.php) – Daniel Apr 27 '13 at 15:16
  • 1
    editted my post and added what I would like the output to be. So the function has to loop a few times to achieve this goal. Thanks for the links, I have to go to the gym now and I'll take a look at them when I get back! Appreciate it – Chickenlal Apr 27 '13 at 15:18
  • The main problem in your code is that you are only going 2 levels in with the 2 `foreach()`, you need to go deeper. You can hardcode this for X levels (bad idea) or use some recursive method as Daniel suggested. – Francisco Presencia Apr 27 '13 at 15:22
  • 1
    @FrankPresenciaFandos That's why I'm here. I could hardcode it of course for a fixed number of levels. But the problem is that where this piece of code has to be implemented I can't know how many levels there are going to be. So I hope recursive method can fix this :-) – Chickenlal Apr 27 '13 at 15:28

1 Answers1

1

Check out this little function I wrote. It does exactly what you need.. This is the recursive method. You can check it out for any levels.

<?php
function arrayRec($array, $intent) {
    $intent .= "_";
    foreach($array as $key=>$value) {
        echo $intent.$key."</br>";
        arrayRec($value, $intent);
    }   
}
$tree = array("1" => array("11" => array(), "12" => array(), "13" => array()), "2" => array(), "3" => array("31" => array("311" => array()), "32" => array()), "4");
arrayRec($tree);
?>

This results in something like:

_1
__11
__12
__13
_2
_3
__31
___311
__32
_4
Miro Markaravanes
  • 3,285
  • 25
  • 32
  • I will cehck it out for sure! Thanks – Chickenlal Apr 27 '13 at 16:41
  • Looks like this is what I need, I'm only getting this error: Invalid argument supplied for foreach() Not exactly sure why it's complaining about that... But thanks man !! I'm gonna get it working with this function! – Chickenlal Apr 27 '13 at 19:19
  • Follow the pattern as in the $tree variable , and you will get no errors! Thx for accepting – Miro Markaravanes Apr 27 '13 at 19:37
  • I fixed the function so it works perfectly with my design. I will add it as the answer – Chickenlal Apr 27 '13 at 19:43
  • thanks man!I could edit exactly what I needed. I wasn't really too familiar with recursive functions and it never came to my mind. The output is exactly what I needed! I will update the main post with the answer. – Chickenlal Apr 27 '13 at 19:48