NewBie NewBie -3 years ago 122
PHP Question

Display array list elements in dropdown list in php

For example, I have such array:

$someArray = Array (
[stationList] => Array (
[0] => Array (
[stationName] => A.S.Peta Bypass
[stationId] => -1 )
[1] => Array (
[stationName] => Aala
[stationId] => -1 )

Now U want to display the array elements(stationName) in a dropdown list in php, I used the below code:

<select id="txtLabourId">
<option selected="selected">Choose one</option>

foreach($someArray as $name) { ?>
<option value="<?php echo $name['stationName'] ?>"><?php echo $name['stationName'] ?></option>
} ?>

But its giving the error:

Undefined index stationName on line 222

How to resolve this? Any help appreciated.

Answer Source

The $someArray array contains another array, so start your foreach loop from the inner array like this

    foreach($someArray['stationList'] as $station) { 
      <option value="<?php echo $station['stationId'] ?>"><?php echo $station['stationName'] ?></option>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download