Answers:
foreach( $codes as $code and $names as $name ) { }
那是无效的。
您可能想要这样的东西...
foreach( $codes as $index => $code ) {
echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}
另外,使代码成为$names
数组的键会容易得多...
$names = array(
'tn' => 'Tunisia',
'us' => 'United States',
...
);
foreach
一次只能操作一个阵列。
数组的结构方式,可以将array_combine()
它们分为键值对数组和foreach
单个数组:
foreach (array_combine($codes, $names) as $code => $name) {
echo '<option value="' . $code . '">' . $name . '</option>';
}
或者从其他答案中可以看出,您可以改为硬编码关联数组。
用于array_combine()
将阵列融合在一起并遍历结果。
$countries = array_combine($codes, $names);
使用关联数组:
$code_names = array(
'tn' => 'Tunisia',
'us' => 'United States',
'fr' => 'France');
foreach($code_names as $code => $name) {
//...
}
我相信使用关联数组是最明智的方法,而不是使用,array_combine()
因为一旦有了关联数组,您就可以简单地使用array_keys()
或array_values()
获得与以前完全相同的数组。
array_combine()
已经产生了一个关联数组,您可能想更清楚地将其初始化为关联数组。
这将从数组创建一个下拉菜单,并自动分配其各自的值。
<?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>';
?>
<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>
<?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>';
?>
这对我有用:
$codes = array('tn', 'us', 'fr');
$names = array('Tunisia', 'United States', 'France');
foreach($codes as $key => $value) {
echo "Code is: " . $codes[$key] . " - " . "and Name: " . $names[$key] . "<br>";
}
走出来...
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
PHP 5.3以上
array_walk($codes, function ($code,$key) use ($names) {
echo '<option value="' . $code . '">' . $names[$key] . '</option>';
});
在PHP 5.3之前
array_walk($codes, function ($code,$key,$names){
echo '<option value="' . $code . '">' . $names[$key] . '</option>';
},$names);
或结合
array_walk(array_combine($codes,$names), function ($name,$code){
echo '<option value="' . $code . '">' . $name . '</option>';
})
在选择
array_walk(array_combine($codes,$names), function ($name,$code){
@$opts = '<option value="' . $code . '">' . $name . '</option>';
})
echo "<select>$opts</select>";
这样的代码是不正确的,因为foreach仅适用于单个数组:
<?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>';
}
?>
或者,更改为:
<?php
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$count = 0;
foreach($codes as $code) {
echo '<option value="' . $code . '">' . $names[count] . '</option>';
$count++;
}
?>
您应该尝试将2数组放入singlr foreach循环中假设我有2数组1. $ item_nm 2. $ item_qty
`<?php $i=1; ?>
<table><tr><td>Sr.No</td> <td>item_nm</td> <td>item_qty</td> </tr>
@foreach (array_combine($item_nm, $item_qty) as $item_nm => $item_qty)
<tr>
<td> $i++ </td>
<td> $item_nm </td>
<td> $item_qty </td>
</tr></table>
@endforeach `
很少有数组也可以像这样迭代:
foreach($array1 as $key=>$val){ // Loop though one array
$val2 = $array2[$key]; // Get the values from the other arrays
$val3 = $array3[$key];
$result[] = array( //Save result in third array
'id' => $val,
'quant' => $val2,
'name' => $val3,
);
}