views:

197

answers:

2

Hi! I have a multidimensional array like this:

array(2) {
  [1]=>
  array(3) {
    ["eventID"]=>
    string(1) "1"
    ["eventTitle"]=>
    string(7) "EVENT 1"
    ["artists"]=>
    array(3) {
      [4]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 1"
        ["description"]=>
        string(13) "artist 1 desc"
        ["links"]=>
        array(2) {
          [1]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist1.com"
          }
          [6]=>
          array(2) {
            ["URL"]=>
            string(24) "http://www.artist1-2.com"
          }
        }
      }
      [5]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 8"
        ["description"]=>
        string(13) "artist 8 desc"
        ["links"]=>
        array(1) {
          [8]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist8.com"
          }
        }
      }
      [2]=>
      array(2) {
        ["ime"]=>
        string(8) "ARTIST 5"
        ["opis"]=>
        string(13) "artist 5 desc"
        ["links"]=>
        array(1) {
          [9]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist5.com"
          }
        }
      }
    }
  }
  [2]=>
  array(3) {
    ["eventID"]=>
    string(1) "2"
    ["eventTitle"]=>
    string(7) "EVENT 2"
    ["artists"]=>
    array(3) {
      [76]=>
      array(2) {
        ["name"]=>
        string(9) "ARTIST 76"
        ["description"]=>
        string(14) "artist 76 desc"
        ["links"]=>
        array(1) {
          [13]=>
          array(2) {
            ["URL"]=>
            string(23) "http://www.artist76.com"
          }
        }
      }
      [4]=>
      array(2) {
        ["name"]=>
        string(8) "ARTIST 4"
        ["description"]=>
        string(13) "artist 4 desc"
        ["links"]=>
        array(1) {
          [11]=>
          array(2) {
            ["URL"]=>
            string(22) "http://www.artist4.com"
          }
        }
      }
    }
  }
}

I would like to make html output like this:

--

EVENT 1
ARTIST 1
artist 1 desc
http://www.artist1.com, http://www.artist1-2.com

ARTIST 8
artist 8 desc
http://www.artist8.com

ARTIST 5
artist 5 desc
http://www.artist5.com

--

EVENT 2
ARTIST 76
artist 76 desc
http://www.artist76.com

ARTIST 4
artist 4 desc
http://www.artist4.com

--

etc.

I'm confused about digging deeper and deeper in arrays, especially when my array keys are not sequential numbers but IDs of artist/link/etc.
These arrays will kill me, honestly! =)

Thanks for any help in advance!!!

+3  A: 

The foreach statement will take care of all of this for you, including the associative hashes. Like this:

foreach($array as $value) {
    foreach($value as $key => $val) {
        if($key == "links") {

        }
        /* etc */
    }
}
Jed Smith
+6  A: 

You're best using the foreach construct to loop over your array. The following is untested and is off the top of my head (and probably therefore not as thought through as it should be!) but should give you a good start:

foreach ($mainArray as $event)
{
  print $event["eventTitle"];

  foreach ($event["artists"] as $artist)
  {
     print $artist["name"];
     print $artist["description"];

     $links = array();
     foreach ($artist["links"] as $link)
     {
       $links[] = $link["URL"];
     }
     print implode(",", $links);
  }
}
richsage
Thank you both for helping!!! This example looks better to me so I choose this for answer, but I will reward the other answer too because I find it useful as much as this one =)
errata