PHP将foreach数组转换为选项值

时间:2016-03-30 20:15:36

标签: php select option

无需提供有关正在使用的API的太多信息,我正在尝试获取选项值以匹配选项标记内包含的内容。

这样的事情:

<option value="Foo">Bar</option>

这是PHP:

<?php
    $counter = 0;
    $numbers = $client->account->available_phone_numbers->getList('US', 'Local', array(
    "AreaCode" => $_POST["areacode"]));
    echo "<select>";
    foreach($numbers->available_phone_numbers as $number) {
        echo "<option value=''>";
        echo $number->phone_number;
        $counter++;
        echo "</option>";
        echo "<br>";
    }
    echo "</select>";
    echo "<br>";
    echo $counter;
?>

使用此表格:

<form action="index.php" method="post">
    Area Code:<br>
    <input type="text" name="areacode" value=""><br>
    <input type="submit" value="Submit">
</form>

4 个答案:

答案 0 :(得分:2)

您的语法不正确。

echo '<select>';
foreach($numbers->available_phone_numbers as $number) {

    echo '<option value="' .$number->phone_number .'">';
    echo $number->phone_number;
    echo '</option>';
}
echo '</select>'; 

此外,您的br元素中不应包含select标记。

答案 1 :(得分:1)

echo "<option value=''>";更改为echo "<option value='{$number->phone_number}'>";

答案 2 :(得分:0)

我认为您需要在表单标记内移动选择,以便也提交所选数据。

Class A {
   private ScheduledExecutorService scheduler;
   private int nbThread=3;
   public A(){
      scheduler = Executors.newScheduledThreadPool(nbThread);
   }

   public start(){
      for (int i = 0; i < nbThread; i++) {
          scheduler.execute(new B();
      }
  }

希望这个帮助

答案 3 :(得分:0)

<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local',        
array(
"AreaCode" => $_POST["areacode"]
));
?>
<form action="index.php" method="post">
<?php
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option>" . $number->phone_number . "</option>";
$counter++;
}
echo "</select>";

?>
<input type="submit" value="Submit">
</form>