How can I print the elements of an associative array with a for loop?

96 views Asked by At
$eventname = array(
        "April" => array("April Fool", 4),
        "May" => array("Labour Day", 1)
    );

How can I apply for loop and print the element of the array?

3

There are 3 answers

2
Brian On BEST ANSWER

Your date for the event needs to be wrapped in quotes.

$eventname = array(
  "April"=>array("April Fool","4"),
  "May"=>array("Labour Day","1")
);

foreach($eventname as $month => $event) {
  Echo($month." ".$event['1']. " is ".$event['0']);
}
0
OllyBarca On

Use a foreach to loop through the array:

foreach($eventname as $event => $id) {
    echo $event;
}
0
AudioBubble On

You can try this

 foreach ($eventname as $key => $value) {
         echo "Key: $key; <br />"; 
              foreach ($value as $vale)
              echo "Value is : $val <br />";
        }