How to solve php array reference problem left after foreach loop

不言
Release: 2023-04-02 22:46:01
Original
1779 people have browsed it

This article mainly introduces the reference problem of PHP leaving an array after the foreach loop. It has a certain reference value. Now I share it with you. Friends in need can refer to it

eg:


result:


##Explanation:

1.foreach is not block level Domain, array reference ends, $k, $a does not release memory

2. When the first reference cycle ends, $a points to the last element of the array4

3. When the first reference cycle ends Start the loop for the second time (at this time $a already points to the last element and is a reference)

Loop 4 times. The changes in the array are:

1->

[1,2,3,4]

2-> [1,

2,3,4]

3-> [1,2,

3,3] The third time value points to the last element 3, but value is a global reference that pointed to the last element in the last cycle, so when value=3, the last element will also be The value is changed to 3

4-> [1,2,3,3]

Notes:

1. The implementation principle of foreach has the concept of internal pointers

2. When the foreach loop ends, the pointer will point to the last element

How to avoid:

After using &foreach loop, unset ($a)

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the analysis of PHP pipeline plug-in League\Pipeline

How to use PHP to upload multiple videos at the same time Function

The above is the detailed content of How to solve php array reference problem left after foreach loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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