使用 PHP 中的数组自动填充选择框 [英] Auto populate a select box using an array in PHP

查看:36
本文介绍了使用 PHP 中的数组自动填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个选择框

<select><option>...<option></select>

我有一个从 1 到 12 的值数组

and I had an array of values from 1-12

使用 php,我将如何使用该数组自动填充该选择框?

using php, how would I auto populate that select box using that array?

推荐答案

假设数组如下:

$array = array(
  1=>"My first option",
  2=> "My second option"
);

<select>
  <?php foreach($array as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>

这篇关于使用 PHP 中的数组自动填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆