2011-02-11 71 views
3

我有一個PHP數組....PHP問題陣列選擇採用

<?php $state = array("Kentucky", "Ohio", "West Virginia", "Indiana", "Texas"); 

我將如何得到這個阿雷對這一選擇列表報告。

<select size="1" name="state" id="state"> 
<option>Select One</option> 
</select> 
+2

使用`foreach`和`echo`。請參閱[手冊](http://php.net/manual/en/control-structures.foreach.php),例如 – 2011-02-11 00:43:14

回答

1
<select size="1" name="state" id="state"> 
<?php 
foreach($state as $state_name) 
{ 
    echo '<option value="' . $state_name . '">' . $state_name . '</option>'; 
} 
?> 
</select> 
+0

如果它與文本 – RichardTheKiwi 2011-02-11 00:49:19

2
<?php 
$state = array("Kentucky", "Ohio", "West Virginia", "Indiana", "Texas"); 
?> 

<select size="1" name="state" id="state"> 
<option>Select One</option> 
<?php 
    foreach($state AS $val){ 
     echo "<option>$val</option>"; 
    } 
?> 
</select> 

應該做的伎倆

1

您可以爆(擴展陣列)

<select size="1" name="state" id="state"> 
<option>Select One</option> 
<option> 
<?php 
    $state = array("Kentucky", "Ohio", "West Virginia", "Indiana", "Texas"); 
    echo implode('</option><option>', state); 
?> 
</option> 
</select>