Array to unordered list

Discussion in 'PHP' started by jonnyboy74, Jul 21, 2010.

  1. #1
    Hi All

    I am trying to get an multidimensional array to an unordered list so that i can use it as a menu.

    The array is in the format of

    $paths = array (
    '/vendor/devices/classes/view',
    '/vendor/devices/classes/add',
    '/vendor/devices/classes/modify',
    '/vendor/devices/classes/delete',
    '/vendor/devices/view',
    '/vendor/users/view',
    '/vendor/users/add',
    '/vendor/users/modify',
    '/vendor/users/delete',
    '/vendor/content/add',
    '/vendor/content/modify',
    '/vendor/content/delete',
    '/vendor/reporting/view',
    '/vendor/reporting/create',
    '/vendor/reporting/schedule',
    '/vendor/customer/view',
    '/vendor/customer/correspond',
    '/vendor/feedback/view',
    '/vendor/feedback/moderate',
    '/aggregator/vendors/view',
    '/aggregator/vendors/add',
    '/aggregator/vendors/modify',
    '/aggregator/vendors/delete',
    '/aggregator/vendors/users/view',
    '/aggregator/vendors/users/add',
    '/aggregator/vendors/users/modify',
    '/aggregator/vendors/users/delete',
    '/aggregator/reporting/view',
    '/aggregator/reporting/create',
    '/aggregator/reporting/schedule',
    '/aggregator/devices/view',
    '/aggregator/devices/add',
    '/aggregator/devices/modify',
    '/aggregator/devices/delete',
    '/aggregator/devices/classes/view',
    '/aggregator/devices/classes/add',
    '/aggregator/devices/classes/modify',
    '/aggregator/devices/classes/delete'
    );
    and i am trying to get a list to use as a menu in the following format.

    = aggregator
    == devices
    === add
    === classes
    ==== add
    ==== delete
    ==== modify
    ==== view
    === delete
    === modify
    === view
    == reporting
    === create
    === schedule
    === view
    == vendors
    === add
    === delete
    === modify
    === users
    ==== add
    ==== delete
    ==== modify
    ==== view
    === view
    = Vendor
    == content
    === add
    === delete
    === modify
    == customer
    === correspond
    === view
    == devices
    === classes
    ==== add
    ==== delete
    ==== modify
    ==== view
    === view
    == feedback
    === moderate
    === view
    == reporting
    === create
    === schedule
    === view
    == users
    === add
    === delete
    === modify
    === view


    I also need each one of the items to be linked to the original location as its being used as a menu!

    Can anyone help as i have been stuck for while now and have tried to google it.

    many thanks in advance
     
    Last edited: Jul 21, 2010
    jonnyboy74, Jul 21, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Ellaborate is this some specific order, or totally random? and what is it doing? extracting each subdir/dir?

    E.g:

    would:
    /vendor/devices/classes/view

    turn into:
    === vendor
    == devices
    === classes
    ==== view

    or am i lost?
     
    danx10, Jul 21, 2010 IP
  3. jonnyboy74

    jonnyboy74 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Dan

    Yes you have the right idea despite my bad example lol.

    The array is a set of users roles and each one has a page which is located at the path of the array element.

    I tried to use a recursive function to build the list but couldnt get it to work.

    many thanks
     
    jonnyboy74, Jul 21, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
      function map($str)
      {
          $input = range(8, 9);
          /* or array(8, 9); :P */
          $rand_keys = array_rand($input);
          return str_pad($str, $input[$rand_keys], "=", STR_PAD_LEFT);
      }
      
    $paths = array (
    '/vendor/devices/classes/view',
    '/vendor/devices/classes/add',
    '/vendor/devices/classes/modify',
    '/vendor/devices/classes/delete',
    '/vendor/devices/view',
    '/vendor/users/view',
    '/vendor/users/add',
    '/vendor/users/modify',
    '/vendor/users/delete',
    '/vendor/content/add',
    '/vendor/content/modify',
    '/vendor/content/delete',
    '/vendor/reporting/view',
    '/vendor/reporting/create',
    '/vendor/reporting/schedule',
    '/vendor/customer/view',
    '/vendor/customer/correspond',
    '/vendor/feedback/view',
    '/vendor/feedback/moderate',
    '/aggregator/vendors/view',
    '/aggregator/vendors/add',
    '/aggregator/vendors/modify',
    '/aggregator/vendors/delete',
    '/aggregator/vendors/users/view',
    '/aggregator/vendors/users/add',
    '/aggregator/vendors/users/modify',
    '/aggregator/vendors/users/delete',
    '/aggregator/reporting/view',
    '/aggregator/reporting/create',
    '/aggregator/reporting/schedule',
    '/aggregator/devices/view',
    '/aggregator/devices/add',
    '/aggregator/devices/modify',
    '/aggregator/devices/delete',
    '/aggregator/devices/classes/view',
    '/aggregator/devices/classes/add',
    '/aggregator/devices/classes/modify',
    '/aggregator/devices/classes/delete'
    );
      
      $out = '';
      foreach ($paths as $path) {
          $str = ltrim($path, '/');
          
          $array = explode('/', $str);
          
          $array = array_map('map', $array);
          
          $out .= implode("\n", $array) . "\n";
      }
      
      echo "<pre>{$out}</pre>";
    ?>
    PHP:
     
    danx10, Jul 21, 2010 IP
  5. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Maybe I am being dumb, but can't understand why you would use random in your example?

    Try this code, I think it will give you what you want - you will probably need to modify the link it generates in the recursion.

    <?
    
    	$paths = array (
    	'/vendor/devices/classes/view',
    	'/vendor/devices/classes/add',
    	'/vendor/devices/classes/modify',
    	'/vendor/devices/classes/delete',
    	'/vendor/devices/view',
    	'/vendor/users/view',
    	'/vendor/users/add',
    	'/vendor/users/modify',
    	'/vendor/users/delete',
    	'/vendor/content/add',
    	'/vendor/content/modify',
    	'/vendor/content/delete',
    	'/vendor/reporting/view',
    	'/vendor/reporting/create',
    	'/vendor/reporting/schedule',
    	'/vendor/customer/view',
    	'/vendor/customer/correspond',
    	'/vendor/feedback/view',
    	'/vendor/feedback/moderate',
    	'/aggregator/vendors/view',
    	'/aggregator/vendors/add',
    	'/aggregator/vendors/modify',
    	'/aggregator/vendors/delete',
    	'/aggregator/vendors/users/view',
    	'/aggregator/vendors/users/add',
    	'/aggregator/vendors/users/modify',
    	'/aggregator/vendors/users/delete',
    	'/aggregator/reporting/view',
    	'/aggregator/reporting/create',
    	'/aggregator/reporting/schedule',
    	'/aggregator/devices/view',
    	'/aggregator/devices/add',
    	'/aggregator/devices/modify',
    	'/aggregator/devices/delete',
    	'/aggregator/devices/classes/view',
    	'/aggregator/devices/classes/add',
    	'/aggregator/devices/classes/modify',
    	'/aggregator/devices/classes/delete'
    	);
    
    
    	$menu = Array();
    	foreach ($paths AS $path)
    	{
    
    		$sub_paths = explode("/", ltrim($path,"/"));
    
    		$tmp = $sub_paths[count($sub_paths)-1];
    
    		for ($i = count($sub_paths)-1; $i>=0; $i--)
    		{
    			$tmp = Array($sub_paths[$i]=>$tmp);
    
    		}
    		$menu = array_merge_recursive($menu,$tmp);
    	}
    	
    	displayMenu($menu);
    
    
    	function displayMenu($menu, $path="")
    	{
    		echo "<ul>";
    		foreach ($menu AS $key=>$item)
    		{
    			if (is_array($item))
    			{
    				echo "<li style='font-weight: bold;'>".$key."</li>";
    				displayMenu($item, $path.="/" . $key);
    			}
    			else echo "<li><a href='?path=" . $path . "&action=" . $item . "'>" . $item ."</a></li>";
    		}
    		echo "</ul>";
    	}
    
    
    ?>
    PHP:
     
    bencummins, Jul 21, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    To be honest, your not being dumb, its me whos probably being dumb, as I misunderstood the objective. I assumed the list was completely random (refer to my second post).
     
    danx10, Jul 21, 2010 IP