Home > Backend Development > PHP Tutorial > Why does php foreach need to copy an array for operation?

Why does php foreach need to copy an array for operation?

WBOY
Release: 2016-08-18 09:16:29
Original
1235 people have browsed it

What is the purpose? Looking for the process

Reply content:

What is the purpose? Looking for the process

What do you mean? Pardon my ignorance, but what do you mean?

PHP foreach does not need to copy an array to operate. Where did you see that you need to copy an array?

PHP foreach traverses a copy of the array. I think it is probably to avoid an infinite loop caused by modifying (adding items) while traversing?
Just a guess. . .

The questioner wants to know the internal implementation of foreach. It copies a temporary variable for operation instead of operating the actual array

I have always been confused about this question. The following answer is for reference only, haha

Reference for in-depth understanding of php kernel

<code> [鸟哥的php foreach](http://www.laruence.com/2008/11/20/630.html)
 </code>
Copy after login

Let’s look at an example to prove that foreach does copy a temporary variable to operate in the loop

<code class="php">
$arr = array(1,2,3,4,5);

//该数组的下一元素为 2
echo next($arr);//2

//该数组的最后元素为 5
echo end($arr);//5

//即使上一步调用end将数组指针移到最后一位, 但在foreach开始执行时, 会重置数组指针
foreach($arr as $key => $row) {
    next($arr);
    end($arr);
    echo $key, '=>', $row, "\r\n";//
}
</code>
Copy after login

The results are shown below

<code class="php">2
5
0=>1
1=>2
2=>3
3=>4
4=>5
</code>
Copy after login

As above, when foreach calls next or end before the loop starts, it will change the array pointer position, but when it is called inside foreach, the loop process remains unchanged

In the traversed code, when the array pointer is operated through end, next, etc., the array pointer will not change. This is because when the PHP kernel performs a FETCH operation, the internal pointer of the currently operated array will be stored through an intermediate variable. Each time it is traversed, element, the previously stored pointer position will be obtained first, and then the pointer position will be restored after obtaining the next element. The key lies in the intermediate variables during the execution of FETCH OPCODE

Here you can see that the original array that foreach operates from time to time during the loop is a copy of the original array,

Now let’s answer the main question, why do we need to copy an array to operate instead of operating on the original array? My idea is, if foreach operates on the original array, then change the value of the original array in foreach, or If you add a new element, what will happen? If you think about it this way, you will understand,

<code class="php">foreach ($variable as $key => $value) {
    $variable[''] => mt_rand(1, 111);
}</code>
Copy after login
Related labels:
php
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