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'];    

}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!