Warm tip: This article is reproduced from stackoverflow.com, please click
arrays loops multidimensional-array php

Looping a multidimensional array in php

发布于 2020-04-04 10:15:53

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!!!

Questioner
errata
Viewed
101
richsage 2009-10-12 05:33

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);
  }
}