SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    advanced loops with multi-dimensional array

    I'm fairly good in PHP so I understand the main concept of everything. One problem I cant seem to wrap my head around is random dimensions of multidimensional arrays with for loops using the key and value. Here is an example.

    for example lets say I have something I grabbed from a database and I looped through and placed each entry into an array which looks like this

    array[0]["id"] = something
    array[0]["name"] = name
    array[1]["id"] = something else...
    array[1]["name"] = another name

    I can loop through this by doing something similar to

    PHP Code:
    foreach($array as $key => $value){
      if(
    is_array($value)){
        foreach(
    $value as $key => $value){
          echo 
    $key." ".$value."<br>";
        }
      }
      echo 
    "<br>";

    ** the above will basically list something similar to

    id : something
    name : name

    id : something else...
    name : another name

    This is usually what I use to loop through it...

    But lets say I throw a wrench in, such as array[1][0][0] is another array and array[3][0][1][2] is another array
    So basically I may randomly find arrays hidden deep within and I want the script to continue searching through each section to find another dimension to the array
    However I do not want to have to keep typing a for loops because sometimes I don't know if the array is 4 levels deep or 5 or just 2.
    I also want to have the pleasure of being able to grab the key with the value.

    Does anyone have any idea how to do this or if its even possible? I am thinking it may be possible with a function which keeps calling itself to check until it cant call itself any more. But even if I can do that how do I display it on screen in an organized manner.

    Hopefully someone has the knowledge to do this.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Really and truthfully, if you believe you are completely comfortable at the level you're at, this problem tells me it's time for you to tackle object-oriented PHP. It would make this problem a non-problem, because each level would have methods defined to handle this kind of thing.

    However, that won't help you anytime soon as it'll take a while to learn, so I'll focus on what you've got so far.

    You have the right idea with a function which calls itself. But how would you display hierarchial data with HTML? With nested lists

    There are probably more efficient methods than below, but it's written so that you see what's going on:
    PHP Code:
    function makeNestedList(array $Array){
        
    $Output '<ul>';
        foreach(
    $Array as $Key => $Value){
            
    $Output .= "<li><strong>{$Key}: </strong>";
            if(
    is_array($Value)){
                
    $Output .= makeNestedList($Value);
            }else{
                
    $Output .= $Value;
            }
            
    $Output .= '</li>';
        }
        
    $Output .= '</ul>';
        return 
    $Output;
    }
    $Data = array("Some Info" => array("A" => "a""B" => array("B1" => "b1""B2" => "b2"), "C" => array("C1" => array("C11" => "c11""C12" => "c12""C13" => array("C131" => "c131""C132" => "c132")), "C2" => "c2")));
    echo 
    makeNestedList($Data); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @jake

    How would OOP make this a non-problem versus a precedural solution? Both would be done via recursion, no?

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi,

    Another way to approach this is to use the recursiveiteratoritorator class which I show an example off how to work with dynamic depth arrays in this post... It is part of the PHP SPL so it is one of the faster compiled classes to use in such a case.

    Regards,
    Steve
    ictus==""


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
  •