0

I have that data structure into my array $categories

stdClass Object
(
    [37] => Array
        (
            [term_id] => 37
            [name] => Files 
            [parent] => 0
            [38] => Array
                (
                    [term_id] => 38
                    [name] => Downloads 
                    [parent] => 37
                )

        )

    [29] => Array
        (
            [term_id] => 29
            [name] => Articles 
            [parent] => 0
            [36] => Array
                (
                    [term_id] => 36
                    [name] => General
                    [parent] => 29
                )

            [35] => Array
                (
                    [term_id] => 35
                    [name] => WordPress 
                    [parent] => 29
                )

            [34] => Array
                (
                    [term_id] => 34
                    [name] => Php, Sql 
                    [parent] => 29
                )

        )

    [1] => Array
        (
            [term_id] => 1
            [name] => Uncategorized
            [parent] => 0
        )

)

Now what I like to do is print that data in the following form with indents.

Files
    Downloads
Articles
    General
    WordPress
    Php, Sql
Uncategorized

To achive the above result I use that code

$categories = get_array_content(); // Return the above data

print_category_tree($categories);

function print_category_tree(&$c)
{
    foreach($c as $cat)
    {
        ?>
        <label>
            <input type="checkbox" value="<?php echo $cat['term_id']; ?>" /> <?php echo $cat['name']; ?>
        </label>
        <br />
        <?php

        foreach($cat as $categ)
        {
            if(is_array($categ))
            {
                print_category_tree($categ);
            }
        }
    }
}

I know that something is wrong with that but I can't guess what. Can somebody please help me with multi dimentional array iteration ?

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • Note. In the above example I have a two dimentional array, but that can be as many dimentions as the user like. The two dimentions is not the standard. – KodeFor.Me Oct 18 '11 at 08:31
  • 1
    Why do you know something is wrong with it? – str Oct 18 '11 at 08:32
  • 1
    Please post your code *here*. – deceze Oct 18 '11 at 08:32
  • Because is not working. I have try several diferent references but nothing. Also I have noticed that the speed of the script is extromly slow with that loops. I mean it takes in about 30 to 40 seconds to be completed – KodeFor.Me Oct 18 '11 at 08:33
  • possible duplicate of [PHP foreach() with arrays within arrays?](http://stackoverflow.com/questions/5524227/php-foreach-with-arrays-within-arrays) – Gordon Oct 18 '11 at 08:34
  • possible duplicate of [Multidimensional Array Iteration](http://stackoverflow.com/questions/2207599/multidimensional-array-iteration/2207739#2207739) – Gordon Oct 18 '11 at 08:38
  • @Gordon, Thanks a lot for your notice, but unfortunatly the examples you show me are not solving my problem – KodeFor.Me Oct 18 '11 at 08:42
  • @MerianosNikos can you please point out why you accepted the answer pointing you to RecursiveArrayIterator when both my linked possible duplicates show how to use exactly that which you claimed are not solving your problem? – Gordon Oct 18 '11 at 09:25

2 Answers2

2

Use http://www.php.net/manual/en/class.recursivearrayiterator.php

Marc
  • 6,749
  • 9
  • 47
  • 78
  • 1
    Read http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Gordon Oct 18 '11 at 08:45
0

You call the function with two params:

print_category_tree($categ, $forum_id);

But your function takes only one param:

function print_category_tree(&$c)

And you are using the & in the function. You might want to test it without.

function print_category_tree($c)
Dinkheller
  • 4,631
  • 5
  • 39
  • 67
  • I have remove many parts of the normal code for simplifyed looking. I have leave only the parts of the code that are required to get the RID of my problem – KodeFor.Me Oct 18 '11 at 08:58