0

Lets say I have the array down here. I left out a few maps and files as this should be enough to make my point. There is no max depth to the array so there could be even more.

Array
(
    [media] => Array
        (
            [documents] => Array
                (
                    [0] => add.php
                )    
            [music] => Array
                (
                    [albums] => Array
                        (
                            [0] => add.php
                        )    
        )    
    [overview] => Array
        (
            [0] => overview.php
        )
) 

What I would like to get is something like the following:

<ul>
    <li>Media
        <ul>
            <li>Documents
                <ul>
                    <li><a href="/media/documents/add.php">Add</a></li>
                </ul>
            </li>

            <li>Music
                <ul>
                    <li>Albums
                        <ul>
                            <li><a href="/media/music/albums/add.php">Add</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>

    </li>Overview
        <ul>
            <li><a href="/overview/overview.php">Overview</a></li>
        </ul>
    </li>
</ul>

I found php create navigation menu from multidimensional array dynamically but imo the accepted answer has quite a lot garbage and the result isn't quite of what I need. If you would like to know how the array is generated please let me know.

Thanks in advance for helping me

3
  • What have you tried? If you show your code then people will be able to give you some pointers. If you're starting from scratch, I'd suggest a recursive function to deal with the complexities your array may have. Commented Jun 30, 2015 at 8:24
  • @GeoffAtkins as you might have red I tried the answers of the linked question. And I didn't want to clutter the question with every attempt. Commented Jun 30, 2015 at 8:26
  • There's more than one answer in that other thread... between all of them, you should be able to adapt something to your particular situation. Do you require us to write your app for you? Or do you have some specific detailed problem you need help with? Commented Jun 30, 2015 at 8:27

2 Answers 2

3

You need to use a recursive function that loops through your array. Something like this:

function outputMenu(array $array, $baseUrl = '/')
{
    $html = '';
    foreach ($array as $key => $item)
    {
        if (is_array($item))
        {
            $html .= '<li>'.$key.'<ul>';
            $html .= outputMenu($item, $baseUrl.$key.'/');
            $html .= '</ul></li>';
        }
        else
        {
            $html .= '<li><a href="'.$baseUrl.$item.'">'.ucfirst(substr($item, 0, -4)).'</a></li>';
        }
    }
    return $html;
}

echo outputMenu($array);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks exactly what I needed and without the garbage. Also learned a new function ucfirst()
0
$array = array(
    'media'=>array('documents'=>array('add.php'),
                    'music'=>array('albums'=>array('add.php'))),
    'overview'=>array('overview.php')
);
print_link($array);
function print_link($arre){
    foreach($arre as $key => $arr){
        if(is_array($arr)){
        echo '<li>'. $key .'<ul>';
            print_link($arr);//echo '<li>'.$arr.'</li>';
        echo '</ul><li>';

        } else {
            echo '<li>'.$arr.'</li>';           
        }

    }

}

you will need a function for this for this task

2 Comments

With procedural code the function has to be defined before you call it. Why did you add this? It's after my answer and adds nothing, in fact it's worse than my answer...
You should tell people not to help when you are preparing an answer :) anyways it worked like a charm for me...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.