1

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>

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

But its giving the error:

Undefined index stationName on line 222

How to resolve this? Any help appreciated.

1
  • 1
    check first with stationList Commented Aug 17, 2017 at 8:37

4 Answers 4

1

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

<?php
    foreach($someArray['stationList'] as $station) { 
?>
      <option value="<?php echo $station['stationId'] ?>"><?php echo $station['stationName'] ?></option>
<?php
    } 
?>   
Sign up to request clarification or add additional context in comments.

Comments

1

You should add the inner array stationList into loop.

Your loop code should be like this:

foreach($someArray['stationList'] as $name)

Comments

0

You need to give stationList to your foreach, not the wrapping array, so:

foreach($someArray['stationList'] as $name) { ...

Comments

0
value type "; echo "$val"; echo "".gettype($val).""; } ?>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.