-4

我想使用两个数组生成一个选择框,一个包含国家代码,另一个包含国家名称。

这是一个例子:

<?php

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

foreach( $codes as $code and $names as $name ) {

echo '<option value="' . $code . '">' . $name . '</option>';

}

?>

这种方法对我不起作用。有什么建议么?

谢谢

4

8 回答 8

5

It has to be 2 arrays? is more easy if you just use an associative array.

like:

$countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');
foreach($countries as $code=>$name) {
     echo '<option value="' . $code . '">' . $name . '</option>';    
}

if you already have those two arrays, you can combine them:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$countries = array_combine($codes, $names);

Here is a demo: http://codepad.org/nYjCk3uQ

于 2013-08-07T05:22:19.983 回答
2

if you are same size of array you can use like below

<?php

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');
$i=0
foreach( $names as $name ) {

echo '<option value="' . $codes [$i] . '">' . $name . '</option>';
  $i = $i+1    
}

?>
于 2013-08-07T05:22:40.413 回答
2

如果两个数组都被完美索引,那么您可以单独迭代一个数组

for($i=0; $i<count($codes);$i++) {

echo '<option value="' . $code[$i] . '">' . $name[$i] . '</option>';

}

仅当两个数组的值都具有匹配的索引时,这才有效

于 2013-08-07T05:24:39.000 回答
2

全部经过全面测试

从数组创建动态下拉列表的 3 种方法。

这将从数组创建一个下拉菜单并自动分配其各自的值。

方法#1(普通数组)

<?php

$names = array('tn'=>'Tunisia','us'=>'United States','fr'=>'France');

echo '<select name="countries">';

foreach($names AS $let=>$word){
    echo '<option value="'.$let.'">'.$word.'</option>';
}
echo '</select>';
 
?>


方法#2(普通数组)

<select name="countries">

<?php

$countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');
foreach($countries as $select=>$country_name){
echo '<option value="' . $select . '">' . $country_name . '</option>';
}
?>

</select>


方法#3(关联数组)

<?php

$my_array = array(
     'tn' => 'Tunisia',
     'us' => 'United States',
     'fr' => 'France'
);

echo '<select name="countries">';
echo '<option value="none">Select...</option>';
foreach ($my_array as $k => $v) {
    echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
?>
于 2013-08-07T05:33:14.230 回答
1

对两个数组使用 for 循环:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$i = 0;
$length = count($codes);
for($i;$i<$length;$i++) {
    echo '<option value="' . $code[$i] . '">' . $name[$i] . '</option>';
}

这对你有用。

另一种方法在这里:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$newarr = array_combine($codes,$names);
foreach($newarr as $index => $value){
    echo '<option value="' . $index . '">' . $value . '</option>';
}
于 2013-08-07T05:23:01.723 回答
1
foreach( $codes as $index => $code ) {
   echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}
于 2013-08-07T05:23:42.843 回答
0

我会这样处理它:

$names = array('Tunisia' : 'tn','United States' :'us','France': 'fr');
foreach($names as $key=>$value) {
echo '<option value="' . $value . '">' . $key . '</option>';
}

另一种方法可以是:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$count=0
foreach( $names as $name ) {
echo '<option value="' . $codes [$i] . '">' . $name . '</option>';
  $count = $count + 1;  
}
于 2013-08-07T05:23:32.040 回答
0

尝试这个

 foreach( $codes as $index => $code ) {
    echo '<option value="' . $code . '">' . $names[$index] . '</option>';
     }
于 2013-08-07T05:24:16.373 回答