1

I've $menu_array3 as

Array
(
    [9] => Array
        (
            [category_name] => cat1
            [category_id] => 9
            [children] => Array
                (
                    [12] => Array
                        (
                            [category_name] => test5
                            [category_id] => 12
                        )

                    [10] => Array
                        (
                            [category_name] => cat2
                            [category_id] => 10
                            [children] => Array
                                (
                                    [15] => Array
                                        (
                                            [category_name] => cat7
                                            [category_id] => 15
                                            [children] => Array
                                                (
                                                    [18] => Array
                                                        (
                                                            [category_name] => cat10
                                                            [category_id] => 18
                                                        )

                                                    [16] => Array
                                                        (
                                                            [category_name] => cat8
                                                            [category_id] => 16
                                                        )

                                                )

                                        )

                                    [17] => Array
                                        (
                                            [category_name] => cat9
                                            [category_id] => 17
                                        )

                                )

                        )

                )

        )

    [11] => Array
        (
            [category_name] => cat3
            [category_id] => 11
            [children] => Array
                (
                    [13] => Array
                        (
                            [category_name] => cat5
                            [category_id] => 13
                        )

                )

        )

)

with ref to this answer I tried to build navigation menu using

function build_nav($category_name, $data)
{
    $result = array();

    foreach ($data as $row)
    {
        if ($row['category_name'] == $category_name)
        {
            $result = "<li>" . $row['category_name'] . "</li>"; 
            $result= build_nav($row['category_name'], $data);
        }
    }
    return $result;
}
$menu="<ul>";
$menu.=build_nav('cat1', $menu_array3);
$menu.="</ul>";
echo "menu <pre>"; print_r($menu); echo "</pre>";

But i was stopped by

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 261900 bytes)

I request you to correct me with suggestions. Thanks in advance Edit I found solution to build navigation menu directly from db at https://stackoverflow.com/a/3380296/1528701

Community
  • 1
  • 1
now-r-never
  • 183
  • 2
  • 4
  • 18
  • You hand over the full `$data` structure for every iteration of the recursive processing steps. So each level of recursion starts with the same data which obviously means that this is an endless loop copying the data again and again. This _must_ lead to the script getting killed by the http server. Instead of `$data` you must hand over only the part of data actually inside the category you are interested in for the recursion. In this case this probably is the `children` entry. – arkascha May 02 '14 at 06:53
  • @arkascha Thanks! If i write `array_pop()` its children will be removed even before they are taken in for formatting. What can be other way around it to remove the processed data! – now-r-never May 02 '14 at 06:57
  • Just use `$data['children']`, or did I miss something? It also would be more elegant to use references for those recursive calls instead of copy-by-value. Also you will not get a hierarchical menu as you want to with the current code. But you will figure that out yourself once you manage to get a usable output. – arkascha May 02 '14 at 07:05
  • @arkascha i tried `$result= build_nav($row['category_name'], $row['category_name']['children']);` it didn't work – now-r-never May 02 '14 at 07:33
  • Sorry, but "it didn't work" is not an expression that helps in any way. Since you are programming you are able to debug this. If you don't and just try and say "it didn't work", then sorry, it is not my task to solve this for you. Sit down, analyse what is going on by either dumping values or, more advanced, by using a debugger. you have to _understand_ what your own code does. Trial-and-error has never been a programming skill. – arkascha May 02 '14 at 09:47

1 Answers1

0

The reason for this error may be that you are calling your function build_nav() inside the foreach loop. This means that for every value in data, build_nav() will be trigered in which the foreach loop will again be triggerd and so on. So the main answer is : You've created an infinite loop.

I think you may have misinterpreted the answer which inspired you to make this function. In that case they are using a parent-id. This may help you in your search. Also notice that they don't use a deeply nested array like you are using. Take a good look and you'll find your solution.

Audax
  • 91
  • 1
  • 4