两个数组的foreach循环 [英] Two arrays in foreach loop
本文介绍了两个数组的foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用两个数组,一个包含全国codeS生成一个选择框,另一个包含国名。
这是一个例子:
< PHP$codeS =阵列('TN','我们','FR');$名称=阵列('突尼斯','美国','法国');的foreach($codeS为$code和$名称为$名){呼应'<期权价值='。$$ C $角'>' 。 $名称。 '< /选项>';}
这个方法并没有为我工作。有什么建议?
解决方案
的foreach($ codeS为$code和$名称为$名){}
这是无效的。
您可能希望这样的事情...
的foreach($ codeS为$指数=> $code){
呼应'<期权价值='。$$ C $角'>' 。 $名称[$指数。 '< /选项>';
}
另外,它会更容易使codeS你的 $名称中的关键
阵列...
$ =名称阵列(
TN'=> '突尼斯',
'我们'= GT; '美国',
...
);
I want to generate a selectbox using two arrays, one containing the country codes and another containing the country names.
This is an example:
<?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>';
}
This method didn't work for me. Any suggestions?
解决方案
foreach( $codes as $code and $names as $name ) { }
That is not valid.
You probably want something like this...
foreach( $codes as $index => $code ) {
echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}
Alternatively, it'd be much easier to make the codes the key of your $names
array...
$names = array(
'tn' => 'Tunisia',
'us' => 'United States',
...
);
这篇关于两个数组的foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文