PHP array pointer exploration 2

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page of PHP array pointer exploration 2. It introduces the knowledge, skills, experience, and some PHP Source Code related to PhP.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 338285 'scrolling = 'no'> in "php array pointer exploration", we discuss the changes of the array pointers of real parameters and form parameters in function calls. Now let's talk about the changes in the array pointers of the assigned array and the assigned array during the assignment process.
Let's give a conclusion first, and then let's use the code to prove this conclusion. $ Arrtmp = $ arr; in this value assignment expression, I call $ arr a value array and $ arrtmp a value array. When assigning values to a number group, if the array pointer of the value-assigned array has already pointed to the end of the array, the array pointer of the value-assigned array will be reset, pointing to the first element of the array; if the array pointer of the value-assigned array does not point to the end of the array but to any valid array element, the array pointer of the value-assigned array will not be reset, instead, it retains the elements it originally points. After a value assignment, the assigned array not only has the value of the assigned array, but also the array pointer of the assigned array points to that element. The assigned array also points to the element with the same value.
Demo1:
<? PHP
$ Arr = array ('var1' => 1, 'var2' => 2, 'var3' => 3, 'var4' => 4, 'var5' => 5 );
While (List ($ key, $ value) = each ($ ARR ))
{
If ($ value = 4) break;
}
Var_dump (current ($ ARR ));

$ Arr1 = $ arr;

Var_dump (current ($ ARR ));
Var_dump (current ($ arr1 ));
?>
The execution result of demo1 is: int (5) int (5) int (5 ). The result shows that the position of the $ arr array pointer has not changed before and after the value assignment. $ arr1 not only has the same value as $ arr, but also has the same element value as $ arr. Now we can use the above conclusions to explain this result. In the while loop, there is an if judgment statement to prevent the $ arr array pointer from pointing to the end of the array, but keep it in a valid position. When $ value = 4, the loop will jump out, And the each function will move the array pointer one byte forward, which leads to the $ arr array pointer pointing to 5th elements, therefore, before assigning a value, current ($ ARR) returns 5. After assigning a value, because the current pointer of $ arr does not point to the end before assigning a value, therefore, after a value is assigned, the array pointer of $ arr is not reset, but its original position is retained. Therefore, after the value is assigned, the result of using current ($ ARR) is still 5. When a value is assigned, $ arr1 not only obtains the value of $ arr, but also points to the same element as $ arr, both of which are 5.
Demo2:
<? PHP
$ Arr = array ('var1' => 1, 'var2' => 2, 'var3' => 3, 'var4' => 4, 'var5' => 5 );
While (List ($ key, $ value) = each ($ ARR ))
{
// If ($ value = 4) break;
}
Var_dump (current ($ ARR ));

$ Arr1 = $ arr;

Var_dump (current ($ ARR ));
Var_dump (current ($ arr1 ));
?>
In demo2, we have commented out the if ($ value = 4) break; sentence. The goal is to use each to point the $ arr array pointer to the end of the array.
Result of demo2 execution: bool (false) int (1) bool (false ). If the element corresponding to the array pointer is 0, "", or is not a valid value, the current function returns false, and the $ arr value is not 0 or, therefore, it can be broken because the array Pointer Points to an invalid element, causing current to return a false value. In other words, after the while loop is completed, the $ arr array pointer has been directed to the end of the array. So we can see that the value of current ($ ARR) before the value assignment is false, and the value of current ($ ARR) after the value assignment is changed to 1, after the value is assigned, $ arr's array pointer is reset, pointing to the first element of the array. The value of current ($ arr1) is false, indicating that $ arr1 reserves the elements pointed to by the array pointer of $ arr before the value assignment.
Demo1 and demo2 can prove the above conclusion.

Therefore, in order not to be affected by the array pointer when traversing the array, it is best to call the reset () function before or after using the each () function to reset the array pointer. In this way, the above problems can be avoided. There is also a function Prev () for operating the array pointer, which is used to move the current position of the array pointer back one bit, it also needs to pay attention, if the array pointer has already pointed to the end of the array, it will not get the expected result.

By the way, when the foreach function is used to traverse an array, it resets the array pointer and points it to the first element of the array. Note that the foreach operation object is the copy value of the array to be traversed, rather than the array itself.

The above-mentioned understanding of array pointers is just a statement of the family. I hope you will not forget to criticize and correct them while leaving your footprints.

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/338285.html pageno: 9.

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.