How to solve the problem of array reference left by PHP after the Foreach Loop

Source: Internet
Author: User
This article mainly describes the PHP after the Foreach loop to leave an array of reference problems, has a certain reference value, now share to everyone, the need for friends can refer to

eg


Result


Explain:

1.foreach is not a block-level domain, the end of an array reference, $K, $a not free memory

2. When the first reference loop ends, the $a points to the last element of the array 4

3. When the second start cycle (where $ A is already pointing to the last element, is a reference)

The change of the loop 4-times array is:

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

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

3-> [3,3] The third value points to the last element 3 but value is a global reference in the last loop that points to the last element so value=3 will change the value of the last element to 3.

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

Precautions:

The 1.foreach implementation principle has an internal pointer concept

2.& 's foreach Loop end pointer points to the last element

How to avoid:

unset ($a) after circulating with &foreach

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.