Yesterday in the database batch import encountered the old problem (foreach+& problem)

Source: Internet
Author: User
The question can be summarized briefly as:
$arr = Array (1,2,3,4,5), foreach ($arr as $key = & $row) {}foreach ($arr as $key + = $row) {}var_dump ($arr);

Array (size=5)  0 = int 1  1 = int 2  2 = int 3  3 = int 4  4 = &int 4

foreach pointer problem, pointer big God, ask to appear!


Reply to discussion (solution)

$arr = Array (1,2,3,4,5), foreach ($arr as $key = = & $row) {}unset ($row); This should not be less, in order to cut off the reference to the array foreach ($arr as $key = = $row) {}var_dump ($arr);
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); This should not be less, in order to cut off the reference to the array foreach ($arr as $key = = $row) {}var_dump ($arr);
Array (5) {
[0]=> Int (1)
[1]=> Int (2)
[2]=> Int (3)
[3]=> Int (4)
[4]=> Int (5)
}
Well, you reset the pointer during the second traversal, I just want to know how the pointer I wrote above works, and one interview was asked, and I didn't understand it, and I wanted to figure it out.

Do you take it for granted?
Where did you see me? "Reset the pointer during the second traversal"?

Do you take it for granted?
Where did you see me? "Reset the pointer during the second traversal"? Unset ($row); Isn't that right? Please make it explicit.

Do you take it for granted?
Where did you see me? "Reset the pointer during the second traversal"? Just do not understand this process, the owner talk about it, or give a link, I read the article is not very clear.

$arr = Array (1,2,3,4,5);
foreach ($arr as $key = & $row) {}//This execution is complete, $arr [4] is a reference and a value of 5
foreach ($arr as $key + $row) {}//This execution means that each time the current value is assigned to $ARR[4], the value of $arr[4] at the end of the fourth loop is 4, and the fifth time is self-assigned, so it is 4.

$arr = Array (1,2,3,4,5), foreach ($arr as $key = = & $row) {}var_dump ($arr); Echo $arr [4]. '
'; foreach ($arr as $key = + $row) {echo $arr [4]. ' _';} Var_dump ($arr);

$arr = Array (1,2,3,4,5);
foreach ($arr as $key = & $row) {}//This execution is complete, $arr [4] is a reference and a value of 5
foreach ($arr as $key + $row) {}//This execution means that each time the current value is assigned to $ARR[4], the value of $arr[4] at the end of the fourth loop is 4, and the fifth time is self-assigned, so it is 4.

$arr = Array (1,2,3,4,5), foreach ($arr as $key = = & $row) {}var_dump ($arr); Echo $arr [4]. '
'; foreach ($arr as $key = + $row) {echo $arr [4]. ' _';} Var_dump ($arr);
Thank you old classmate!!! It is comfortable to have a great God as a consultant!

$arr = Array (1,2,3,4,5), foreach ($arr as $key = = & $row) {}unset ($row); This should not be less, in order to cut off the reference to the array foreach ($arr as $key = = $row) {}var_dump ($arr);
Array (5) {
[0]=> Int (1)
[1]=> Int (2)
[2]=> Int (3)
[3]=> Int (4)
[4]=> Int (5)
}
The moderator is right, otherwise the following $row is bound to the value of the last element of $arr. The second traversal is also.
Plus 6 floor Classmate's answer, will be more enlightened!
$arr = Array (1,2,3,4,5), foreach ($arr as $key = = & $row) {}var_dump ($arr); foreach ($arr as $key + = $row) {    var_ Dump ($arr);}

Array (size=5)  0 = int 1  1 = int 2  2 = int 3  3 = int 4  4 = &int 5int 5array (s ize=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 =&G T 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

I've thought about it, and it's easy to see how it can be explained by a for-loop.

$arr = Array (1,2,3,4,5), foreach ($arr as $key = & $row) {}var_dump ($arr); for ($i =0; $i < 5; $i + +) {    $row = $a rr[$i];} Var_dump ($arr);

Same result
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.