Results 1 to 6 of 6

Thread: printing out multidimensional array

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default printing out multidimensional array

    Hi I am trying to print out the contents of an array, I cannot seem to get it to print the lower levels. In the end I want to make it so the "levels" are clickable links and if there is an sublevel array associated with that value to that it will display the lower level keys... etc etc... i understand I am going to need a new loop, but i cannot seem to get a grasp on how to accomplish this.. Thanks

    Code:
    $arr = array(
    	"level1" => array(
    		"level2", "level2", "level2", "level2", "level2" => array(
    			"level3", "level3", "level3"
    		), 
    		"level2", "level2" => array(
    			"level3", "level3" => array(
    			 	"level4", "level4" => array(
    					"level5", "level5", "level5", "level5"
    				), 
    				"level4", "level4"
    			),
    		), 
    		"level2", "level2"
    	), 
    	"level1", "level1", "level1"
    );
    
    
    	print "<ul>\n";
    	foreach($arr as $key) {
    		echo "\t<li>".$key." - ".$value."</li>\n";
    	}
    	print "</ul>";

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You need a recursive function, something like:
    Code:
    function show_arr($arr, $lvl = 0) {
      $padding = '';
      for($i = 0; $i < $lvl; ++$i)
        $padding .= "\t";
    
      print "$padding<ul>\n";
      if(is_array($arr))
        show_arr($arr, $lvl + 1);
      else
        print "$padding\t<li>$key - $value</li>\n";
      print "$padding</ul>\n";
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    lol wow now I feel dumb. thanks twey, but its giving me an error, as its not finding the array. I have attempted to pass in the array name, and different level's in but neither of them will allow me to print anything but the "-" between the key and value

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, my error:
    Code:
    function show_arr($arr, $lvl = 0) {
      $padding = '';
      for($i = 0; $i < $lvl; ++$i)
        $padding .= "\t";
    
      print "$padding<ul>\n";
      foreach($arr as $key => $val)
        if(is_array($val))
          show_arr($arr, $lvl + 1);
        else
          print "$padding\t<li>$key - $val</li>\n";
      print "$padding</ul>\n";
    }
    There's generally no need to modify $lvl, it's used internally.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    its still not recognizing the array... its lagging the system and blowing up the cpu usage :/

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function show_arr($arr, $lvl = 0) {
      $padding = '';
      for($i = 0; $i < $lvl; ++$i)
        $padding .= "\t";
    
      print "$padding<ul>\n";
      foreach($arr as $key => $val)
        if(is_array($val))
          show_arr($val, $lvl + 1);
        else
          print "$padding\t<li>$key - $val</li>\n";
      print "$padding</ul>\n";
    }
    The joy of typos.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •