如何使用for循环打印关联数组的元素?

时间:2015-06-08 19:24:52

标签: php arrays for-loop associative-array

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

如何申请循环并打印数组元素?

3 个答案:

答案 0 :(得分:0)

使用foreach遍历数组:

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

答案 1 :(得分:0)

您的活动日期需要用引号括起来。

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

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

答案 2 :(得分:0)

你可以试试这个

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