我已经编写了一个简单的数组,我将使用它来打印一个html列表选项集,其中包含一个选定的元素。如果我试图在页面中打印多个列表,问题就会出现,因为只有第一个列表被正确打印,为什么?
<?php
$units = array (
'0' => 'Units',
'kJ' => 'Kilojoule: kJ',
'g' => 'Grams: g',
'mg' => 'Milligrams: mg',
'mcg' => 'Micrograms: mcg, µg');
function unit_select_option ($attributes, $code = "") {
global $units;
$html = "<select title=\"Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;\" $attributes>\r";
while (list($key, $name) = each($units)) {
if ($key == "0") {
$html .= " <option title=\"$name\" value='$key'>$name</option>\r";
} else if ($key == $code) {
$html .= " <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r";
} else {
$html .= " <option title=\"$name\" value='$key'>$key</option>\r";
}
}
$html.= "</select>\r";
return $html;
}
print unit_select_option ('class="units_select"', "g");
print unit_select_option ('class="units_select"', "mg");
print unit_select_option ('class="units_select"', "mcg");
?>代码应该没什么奇怪的,但我没有发现问题,因为页面没有返回任何错误。
html code:
<select title="Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;" class="units_select">
<option title="Unità" value='0'>Unità</option>
<option title="Kilojoule: kJ" value='kJ'>kJ</option>
<option title="Grammi: g" selected="selected" value='g'>g</option>
<option title="Milligrammi: mg" value='mg'>mg</option>
<option title="Microgrammi: mcg, µg" value='mcg'>mcg</option>
</select>
<select title="Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;" class="units_select">
</select>
<select title="Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;" class="units_select">
</select>发布于 2010-01-17 21:44:20
来自each()
返回数组中的当前键和值对,并使数组游标前进。
执行each()后,数组游标将停留在数组的下一个元素上,如果游标到达数组的末尾,则位于最后一个元素之后。如果您想使用each再次遍历数组,则必须使用reset()。
所以:
function unit_select_option ($attributes, $code = "") {
global $units;
$html = "<select title=\"Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;\" $attributes>\r";
reset($units);
while (list($key, $name) = each($units)) {
if ($key == "0") {
$html .= " <option title=\"$name\" value='$key'>$name</option>\r";
} else if ($key == $code) {
$html .= " <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r";
} else {
$html .= " <option title=\"$name\" value='$key'>$key</option>\r";
}
}
$html.= "</select>\r";
return $html;
}我倾向于避免使用each(),因为它是不可重入的,这意味着如果在你的循环中调用了在同一数组上使用它的其他东西,它会影响你的外部调用。不太好。只使用foreach循环会更好:
function unit_select_option ($attributes, $code = "") {
global $units;
$html = "<select title=\"Kilojoule: kJ; Grammi: g; Milligrammi: mg; Microgrammi: mcg, µg;\" $attributes>\r";
foreach ($units as $key => $name) {
if ($key == "0") {
$html .= " <option title=\"$name\" value='$key'>$name</option>\r";
} else if ($key == $code) {
$html .= " <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r";
} else {
$html .= " <option title=\"$name\" value='$key'>$key</option>\r";
}
}
$html.= "</select>\r";
return $html;
}你可以避免所有这些问题。
发布于 2010-01-17 21:43:36
each()使内部数组游标前进。因为$units是一个全局变量,所以您第一次调用unit_select_option()时会将游标移动到$units的末尾,并将游标保留在那里供后续调用使用。
您需要在unit_select_option()末尾使用reset($units);倒回数组。
PHP文档:
发布于 2010-01-17 21:47:21
您应该重置数组指针:使用reset()
但是为什么不使用foreach循环呢?
foreach($units as $key => $name){ ... }不要使用global,它是邪恶的。在函数体中将$units数组声明为静态。
https://stackoverflow.com/questions/2081120
复制相似问题