我正在阅读PHP手册(特别是each()函数)并遇到以下警告:
注意
因为将数组分配给另一个变量会重置原始数组的指针,如果我们将$ fruit分配给循环内的另一个变量,上面的示例会导致无限循环.
一个例子:
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
好的.这说得通.但我决定做一个简单的测试:
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
foreach ($fruit as $key => $name) {
printf("[%s] => [%s]\n", $key, $name);
}
$fruit2 = $fruit;
echo current($fruit);
?>
Run Code Online (Sandbox Code Playgroud)
结果是预期的:指针已被重置.我的问题是指针是否仅在数组结束后重置?
例如:
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
foreach ($fruit …Run Code Online (Sandbox Code Playgroud)