Why does phpforeach need to copy an array?

Source: Internet
Author: User
What is the purpose? what is the purpose?

Reply content:

What is the purpose of the process?

What do you mean! Excuse me, what do you mean?

PHP foreach does not need to copy an array. where do you need to copy an array?

PHP foreach traverses the copy of the array. I think it is to avoid the endless loop caused by traversing and modifying (adding items?
Just guess...

The subject wants to knowforeachThe internal implementationTemporary variableInstead of operating the actual array.

I have been very ignorant of this question. the following answers are for reference only. haha

For more information, see

[Laruence's php foreach] (http://www.laruence.com/2008/11/20/630.html)

Let's look at a column proof.foreachIn the Loop, it is indeed a copyTemporary variableTo operate

$ Arr = array (1, 2, 3, 4, 5); // The next element of the array is 2 echo next ($ arr ); // 2 // The last element of the array is 5 echo end ($ arr); // 5 // even if you call end in the previous step to move the array pointer to the last position, however, when foreach starts execution, the array pointer foreach ($ arr as $ key => $ row) {next ($ arr); end ($ arr); echo $ key, '=>', $ row, "\ r \ n ";//}

The result is as follows:

250=>11=>22=>33=>44=>5

As shown above,foreachCallnextOrendInforeachThe loop process remains unchanged during the call.

In the traversal code, the array pointer will not change through the end, next and other operations on the array, because when the PHP kernel performs the FETCH operation, the internal pointer of the current operation array is stored through the intermediate variable. each time an element is traversed, the pointer position previously stored is obtained first, and the next element is retrieved before the pointer position is restored, the key lies in the intermediate variable in the execution of the fetch opcode.

Here we can see thatforeachThe original array operated from time to time in a loop, instead of copying the original array,

Now I want to answer the primary question back and forth. why do I need to copy an array instead of operating the original array?foreachThe operation is the original array, then inforeachTo change the value of the original array or add new elements, then what will happen,

foreach ($variable as $key => $value) {    $variable[''] => mt_rand(1, 111);}

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.