0

I'm trying to format a php array to a visual tree through a recursive function but for some reason it doesn't seem to work. It only prints the first level. Here's my code.

function recursiveMenu($arr){
    $maketree = "";
    if(is_array($arr)){
        foreach($arr as $key => $val){
            if(is_array($val)){
                $maketree .= "<div class='expcol jexpand'><a>[-]</a>".$key."</div>"
                           . "<div class='section jsection'>";
                recursiveMenu($val);
            }else{
                $maketree .= "<div>".TB.TB." <a href='index.php?action=". $val . "' >"
                          . "<div class='menuitem'>" . $val . "</div>"
                          . "</a></div>";
            }
            $maketree .="</div>";
        }
    }
    return $maketree;
}

function makeMenu($srch) {
    $makemenu = "<div>";
    $makemenu .= recursiveMenu($srch);
    $makemenu .= "</div>";  // </a>
    return $makemenu;
}

Below is my array result from DB:

$srch = Array(
    [Asia] => Array(
            [South] => Array(
                    [India] => Array(
                            [0] => Mumbai
                            [1] => New Delhi
                            [2] => Chennai
                        )
                )
        )

    [Europe] => Array(
            [West] => Array(
                    [Spain] => Array(
                            [0] => Madrid
                        )
                )

            [North] => Array(
                    [Denmark] => Array(
                            [0] => Copenhagen
                        )
                )
        )

    [Americas] => Array(
            [South] => Array(
                    [Brazil] => Array(
                            [0] => Brasilia
                        )
                )

            [North] => Array(
                    [USA] => Array(
                            [0] => Los Angeles
                            [1] => Atlanta
                            [2] => Boston
                            [3] => Houston
                        )
                )

            [Central] => Array(
                    [Mexico] => Array(
                            [0] => Cancun
                            [1] => Mexico city
                        )
                )

        )

    [Africa] => Array(
            [South] => Array(
                    [South Africa] => Array(
                            [0] => Cape Town
                            [1] => Johannesburg
                        )
                )
        )
)

The actual result I get is this (only the first level):

[-]Asia
[-]Europe
[-]Americas
[-]Africa

Does anyone see what's wrong here? Thanks in advance.

1 Answer 1

1

Aren't you forgetting to append the results of the 2nd recursive call to recursiveMenu() to the output. I think you need to change line 8 to :

$maketree .= recursiveMenu($val);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.