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

Community
  • 1
  • 1
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • 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. – Geoff Atkins Jun 30 '15 at 08: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. – SuperDJ Jun 30 '15 at 08: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? – deceze Jun 30 '15 at 08:27

2 Answers2

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);
Styphon
  • 10,304
  • 9
  • 52
  • 86
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

Ankit Pise
  • 1,243
  • 11
  • 30
  • 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... – Styphon Jun 30 '15 at 08:56
  • You should tell people not to help when you are preparing an answer :) anyways it worked like a charm for me... – Ankit Pise Jun 30 '15 at 09:02