May 25, 2012
Looping through a complex php array
Question by techish
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
)
)
Can anyone tell me how to extract event_title
, event_id
and location_id
from this array? I want to display this in the form of a table actually.
Answer by Mischa
To display it in a table, you can do this:
<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>
Answer by Starx
The array is very simple in fact.
foreach($array as $item) {
$event_title = $item['event_title'];
$event_id = $item['event_id'];
$location_id = $item['location_id'];
}