循环通过复杂的PHP数组

时间:2012-05-25 06:35:10

标签: php arrays

Array (
  [0] => Array ( 
    [event_title] => Registration
    [event_id] => 17
    [location_id] => 113
  )
  [1] => Array (
    [event_title] => Sunday Collections
    [event_id] => 67
    [location_id] => 113
  )
  [2] => Array (
    [event_title] => School Tuition
    [event_id] => 68
    [location_id] => 113
  )
)

有人能告诉我如何从此数组中提取event_titleevent_idlocation_id吗?我想以桌子的形式显示它。

5 个答案:

答案 0 :(得分:4)

实际上,数组非常简单

foreach($array as $item) {

    $event_title = $item['event_title'];
    $event_id = $item['event_id'];
    $location_id = $item['location_id'];    

}

答案 1 :(得分:3)

<table><?php
    foreach($array as $arr) {
        echo '<tr><td>'.
            $arr['event_title'].
            '</td><td>'.
            $arr['event_id'].
            '</td><td>'.
            $arr['location_id'].
            '</td></tr>'.PHP_EOL;
     }
?></table>

答案 2 :(得分:3)

要在表格中显示,您可以这样做:

<table>
  <tr>
    <th>Event Title</th>
    <th>Event ID</th>
    <th>Location ID</th>
  </tr>
<?php foreach($array as $item): ?>
  <tr>
    <td><?php echo $item['event_title'] ?></td>
    <td><?php echo $item['event_id'] ?></td>
    <td><?php echo $item['location_id'] ?></td>
  </tr>
<?php endforeach; ?>
</table>

答案 3 :(得分:1)

foreach ($arr as $event){
    echo $event["event_title"] . "\t" . $event["event_id"] . "\t". $event["location_id"] . "\n";
}

答案 4 :(得分:1)

foreach ($array as $row) {
  ...
  echo "<td>{$row['event_title']}</td>";
  ...
}