Home > Backend Development > PHP Tutorial > 昨天在做数据库批量导入的时候遇到的老问题(foreach+&问题)

昨天在做数据库批量导入的时候遇到的老问题(foreach+&问题)

WBOY
Release: 2016-06-23 14:22:38
Original
1001 people have browsed it

问题可以简要的概括为:

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}foreach($arr as $key => $row) {}var_dump($arr);
Copy after login

array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 4
Copy after login

foreach 指针问题,指针大神,求现身!


回复讨论(解决方案)

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}unset($row); //这个不能少,为了切断数组的引用foreach($arr as $key => $row) {}var_dump($arr);
Copy after login
Copy after login
Copy after login
array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=> int(5)
}

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}unset($row); //这个不能少,为了切断数组的引用foreach($arr as $key => $row) {}var_dump($arr);
Copy after login
Copy after login
Copy after login
array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=> int(5)
}
嗯,您在第二次遍历的时候重置了指针,我就是想知道我上面写的那个指针是怎么运作的,有一次面试就被问了,一直没弄明白,想弄明白下。

想当然了吧?
你从什么地方看到我“在第二次遍历的时候重置了指针”?

想当然了吧?
你从什么地方看到我“在第二次遍历的时候重置了指针”? unset($row); 这个不是么?请斑竹大人明示。

想当然了吧?
你从什么地方看到我“在第二次遍历的时候重置了指针”? 就是不懂这个过程,斑竹讲讲吧,或者给个链接,我看的文章都说得不太明白。

$arr = array(1,2,3,4,5);
foreach($arr as $key => &$row) {}//这个执行完毕后,$arr[4]是个引用,值为5
foreach($arr as $key => $row) {}//这个执行过程的意思是:每次将当前值赋值给$arr[4],在在第四次循环完毕时$arr[4]的值为4,第五次就是自己赋值自己,所以为4。

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}var_dump($arr);echo $arr[4].'<br/>';foreach($arr as $key => $row) {	echo $arr[4].'_';}var_dump($arr);
Copy after login
Copy after login

$arr = array(1,2,3,4,5);
foreach($arr as $key => &$row) {}//这个执行完毕后,$arr[4]是个引用,值为5
foreach($arr as $key => $row) {}//这个执行过程的意思是:每次将当前值赋值给$arr[4],在在第四次循环完毕时$arr[4]的值为4,第五次就是自己赋值自己,所以为4。

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}var_dump($arr);echo $arr[4].'<br/>';foreach($arr as $key => $row) {	echo $arr[4].'_';}var_dump($arr);
Copy after login
Copy after login
谢谢老同学!!! 有大神做顾问就是舒服!

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}unset($row); //这个不能少,为了切断数组的引用foreach($arr as $key => $row) {}var_dump($arr);
Copy after login
Copy after login
Copy after login
array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=> int(5)
}
版主说得很对,否则下面的$row一直和$arr的最后一个元素的值绑定。第二次遍历的时候也是。
加上6楼同学的解答,就更加豁然开朗了!
$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}var_dump($arr);foreach($arr as $key => $row) {    var_dump($arr);}
Copy after login

array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 5int 5array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 1array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 2array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 3array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 4array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 4array (size=5)  0 => int 1  1 => int 2  2 => int 3  3 => int 4  4 => &int 4
Copy after login

我又想了下,实际可以用for循环来解释就很容易明白了

$arr = array(1,2,3,4,5);foreach($arr as $key => &$row) {}var_dump($arr);for ($i=0; $i < 5; $i++) {    $row = $arr[$i];}var_dump($arr);
Copy after login

同样结果

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template