例如,我有这样的数组:
$someArray = Array (
[stationList] => Array (
[0] => Array (
[stationName] => A.S.Peta Bypass
[stationId] => -1 )
[1] => Array (
[stationName] => Aala
[stationId] => -1 )
)
)现在,您希望在php中的下拉列表中显示数组元素(StationName),我使用了以下代码:
<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> 但它给出了一个错误:
Undefined index stationName on line 222如何解决这个问题?任何帮助都很感激。
发布于 2017-08-17 08:39:04
$someArray数组包含另一个数组,因此从内部数组启动foreach循环,如下所示
<?php
foreach($someArray['stationList'] as $station) {
?>
<option value="<?php echo $station['stationId'] ?>"><?php echo $station['stationName'] ?></option>
<?php
}
?> 发布于 2017-08-17 08:52:03
应该将内部数组stationList添加到循环中。
循环代码应该如下所示:
foreach($someArray['stationList'] as $name)发布于 2017-08-17 08:39:46
您需要将stationList给您的foreach,而不是包装数组,因此:
foreach($someArray['stationList'] as $name) { ...
https://stackoverflow.com/questions/45730252
复制相似问题