php PHP数组和选择列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3555725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:13:01  来源:igfitidea点击:

PHP array and select list

php

提问by Sergio

I want to use php array for HTML select list. In this case it will be the list of countries but beside of country name that is listed in drop down list I need as a value of each country short country code.

我想将 php 数组用于 HTML 选择列表。在这种情况下,它将是国家/地区列表,但在下拉列表中列出的国家/地区名称旁边我需要作为每个国家/地区短国家/地区代码的值。

The php array that I'm currently using looks like:

我目前使用的 php 数组如下所示:

$wcr=array(
'Angola',
'Antigua & Barbuda',
'Armenia', 
'Austria',
'Azerbaijan', 
 .....
 );

The PHP page that using this array:

使用此数组的 PHP 页面:

<select name="country"><option selected value=""> --------- 
<? $p=1;asort($wcr);reset($wcr);
while (list ($p, $val) = each ($wcr)) {
echo '<option value="'.$p.'">'.$val;
} ?>
</select>

The value should be short country code (something like 'ES', 'US', 'AN', ...) instead of numbers that I have right now as a values in this form. That short country codes I will write in this same PHP array somewhere, if it's possible.

该值应该是简短的国家/地区代码(类似于“ES”、“US”、“AN”等),而不是我现在在这种形式中作为值使用的数字。如果可能的话,我将在同一个 PHP 数组中的某个地方编写那个简短的国家/地区代码。

How can I do that?

我怎样才能做到这一点?

回答by Christophe

Use foreach(), it's the best function to loop through arrays

使用foreach(),循环数组是最好的函数

your array should look like jakenoble posted

你的数组应该看起来像 jakenoble 发布的

<select name="country">
<option value="">-----------------</option>
<?php
foreach($wcr as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
</select>

I also made some minor adjustements to your html code. The first option in the list will be selected by default so no need to specify it ;)

我还对您的 html 代码进行了一些小调整。默认情况下将选择列表中的第一个选项,因此无需指定;)

EDIT: I made some edits after reading your question again

编辑:我再次阅读您的问题后进行了一些编辑

回答by Majid Fouladpour

First make an associated array of country codes like so:

首先制作一个相关的国家代码数组,如下所示:

$countries = array(
  'gb' => 'Great Britain',
  'us' => 'United States',
  ...);

Then do this:

然后这样做:

$options = '';
foreach($countries as $code => $name) {
  $options .= "<option value=\"$code\">$name</option>\n";
}
$select = "<select name=\"country\">\n$options\n</select>";

回答by Jake N

Do you mean like this:

你的意思是这样吗:

 $wcr=array(
 "ANG" = > 'Angola',
 "ANB" = > 'Antigua & Barbuda',
 "ARM" = > 'Armenia', 
 "AUS" = > 'Austria',
 "AZB" = > 'Azerbaijan'
  );

Then in your while loop, $pis your Short code.

然后在你的 while 循环中,$p是你的短代码。

Improved version of Krike's loop, if using my array of short codes as array keys:

Krike 循环的改进版本,如果使用我的短代码数组作为数组键:

<select name="country">
<option value="">-----------------</option>
<?php
    asort($wcr);
    reset($wcr); 
    foreach($wcr as $p => $w):
        echo '<option value="'.$p.'">'.$w.'</option>'; //close your tags!!
    endforeach;
?>
</select>

回答by MRW

If you are using this sort of array as shown by jakenoble I would use foreach().

如果您使用 jakenoble 所示的这种数组,我将使用 foreach()。

$wcr=array(
 "ANG" = > 'Angola',
 "ANB" = > 'Antigua & Barbuda',
 "ARM" = > 'Armenia', 
 "AUS" = > 'Austria',
 "AZB" = > 'Azerbaijan'
  );

So the Foreach would look something like:

所以 Foreach 看起来像:

foreach($wcr as $short_code => $descriptive) {
    ?>
    <option value="<?php echo $short_code; ?>"><?php echo $descriptive; ?></option>
    <?php
}