Pointers in PHP arrays think _php tutorial

Source: Internet
Author: User
Tags null null
Recently encountered a very strange problem in the work, after using each of the functions of an array to traverse, and then pass the array as an argument to a function, inside the function again using each traversal parameter group. To illustrate, two times the purpose of using each function is simply to convert the key in the array into a variable name, and the value value corresponding to the key is converted to the variable. In fact, this work can be achieved using the function extract. Let's go on to talk about what's wrong with using the each function inside the function, and find that some of the variables are null after the traversal, which means that some variables are missing. Do not know the description is clear, or to simplify the work of the problem, with the following code to illustrate it.

 
  1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5); while (List ($key, $value) = each ($arr)) {$ $key = $value;} The next step is critical, and the problem is here, after the traversal, add a value to the array. $arr [' var6 '] = 6;func ($arr);        function func ($arrtmp) {  while (list ($key, $value) = each ($arrtmp))    {   $ $key = $value;    }   Var_dump ($var 1);     Var_dump ($var 2);    Var_dump ($var 3);     Var_dump ($var 4);      Var_dump ($var 5);     Var_dump ($var 6);}? >

Output: null NULL NULL NULL NULL INT (6).

According to the conventional idea, at this time in the function func internal variable $var 1, $var 2, $var 3, $var 4, $var 5, $var 6 should have, but the fact is not, only the variable $var6 has a value, and several other variables are null. What is this for?

The problem is with the array pointers we're going to discuss today. Each of these functions returns the element pointed to by the current array pointer as an array and moves the array pointer forward one bit, pointing to the next array cell. After we have traversed the array $arr using the each function, the internal pointer to the $arr array has pointed to the next bit of the last cell (with no value). At this point we executed the $arr [' var6 '] = 6, adding a new unit to the array, and we know that arrays in memory are definitely contiguous address cells. That is, the value of $arr[' VAR6 '] in memory should be in the cell where the current array pointer points (previously empty). and assigning a value to an array does not move the inner pointer of the array, and after the assignment is complete, the array pointer $arr array from the original to a null to point to an address cell with an actual value present.

There is a rule that an array is passed between functions as a parameter: we know that when a function is called, the system assigns a copy of the argument to the formal parameter (except for the reference call), and for the array, not only the value of the argument is copied, but also the position of the current internal pointer of the real parameter group. If the position of the internal pointer of the argument points to the end of the array, the system resets the internal pointer of the parameter to the first cell of the Shape argument group, or if the position of the argument's internal pointer is not at the end of the array, which points to a valid cell, then the array pointer position of the parameter is pointed to the array cell with the same

If you do not $arr [' var6 '] = 6 This step, 6 variables ($var 1-$var 6) will have a value because after each, the array pointer already points to the end of the array, and when the function func () is called, the system resets the $ARRTMP array pointer. Point it to the first element. But after the $arr [' var6 '] = 6, everything changes, and this action lets the $arr array pointer from the original pointer to a null becomes a valid value (explanation, before and after the assignment, the array pointer points to the address cell has been unchanged, but before the assignment, The address unit has nothing, and the assignment becomes 6). This causes the array pointer of the $arr to point to a valid unit, and when the function func () is called, the system does not reset the $arrtmp array pointer, and the $ARRTMP array pointer will be the same as the $arr array pointer, pointing to his own last cell. Each function is also working from the position of the current array pointer. So the return value of the first result of each function operation is the last element of the array $arrtmp, it moves the array pointer down one bit, while the loop ends, so $arrtmp[' var1 '-$arrtmp [' VAR5 '] are not traversed, The resulting $var1-$var 6 is null.

Array in the process of assignment, the array of assignments and the array of the respective array pointer changes. Let's start with a conclusion, and then we'll use the code to prove it. $arrtmp = $arr; In this assignment expression, I call $arr an array of assignments, and $arrtmp is called an array of assigned values. When an array is assigned a value, if the array pointer of an array of assignments has already pointed to the end of the array, the array pointer to the array after the assignment is reset to the first element of the array, and if the array pointer to the array is not at the end of the array when the assignment is made, it points to any valid array element. Then the array pointer of the assignment array after the assignment is not reset, but retains the element it originally pointed to. After the assignment, the assigned array has not only the value of the array of assignments, but the array pointer to the array of assignments points to that element, and the assigned array also points to the same element in which the value is the same.

Demo1:

 
  1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' Var5 ' =>5), while (List ($key, $value) = All ($arr)) {if ($value = = 4) break;} Var_dump (current ($arr)), $arr 1 = $arr, Var_dump (current ($arr)), Var_dump (current ($arr 1)); >

The result of the DEMO1 is: Int (5) int (5) Int (5). From this result it can be seen that the array pointer position of $arr before and after the assignment has not changed any, $arr 1 not only the value is the same as $arr, and the array pointer points to the same element value. Now use the above conclusions to explain this result, in the while loop, there is an if judgment statement, the purpose is to not let the $arr array pointer to the end of the array, but remain in a valid position. The $value=4 will jump out of the loop, and each of these functions will move the array pointer forward one bit, which results in the $arr array pointer pointing to the 5th element, so before the assignment, the result of current ($arr) is 5, after the assignment, because the value before the assignment The current pointer to Arr does not point to the end, so the $arr array pointer is not reset after the assignment, but it retains its original position, so the result of using the present ($arr) after the assignment is still 5. When assigning a value, $ARR1 not only obtains the value of $arr, but the array pointer points to the same element as the $arr, both of which are 5.

Demo2:

 
  1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5); while (List ($key, $value) = each ($arr)) {    //if ($value = = 4 ) break;} Var_dump (current ($arr)), $arr 1 = $arr, Var_dump (current ($arr)), Var_dump (current ($arr 1)); >

In Demo2 we will break if ($value = = 4); This sentence is commented out, and the purpose is simply to point the array pointer position of $arr to the end of the array by each.

Demo2 Execution Result: bool (FALSE) Int (1) bool (false). If the array pointer corresponds to an element of 0, "", or is not a valid value, the current function returns FALSE, the value of the $arr is not 0 or "", so it can be concluded that the array pointer points to an invalid element and the current returns a false. In other words, you can be sure that the $arr array pointer has pointed to the end of the array after the while loop is complete. So we see that the value of current ($arr) before the assignment is false, and the value of current ($arr) becomes 1 after the assignment, indicating that the array pointer to $arr is reset after the assignment, pointing to the first element of the array. The value of current ($arr 1) is false, indicating that the element to which the array pointer $arr before $ARR1 is retained after the assignment has been assigned.

The above conclusions can be proved by Demo1 and Demo2.

Therefore, in order to traverse an array without being affected by the array pointer, it is best to reset the array pointer before using the each () function or after calling the function reset (). This will prevent the above problems from happening. There is also a function prev () that operates an array pointer, which is to take the current position of the array pointer back one bit, it also needs to be noted that if the array pointer already points to the end of the array, then it will not get the desired result.

By the way, the foreach function, when using the Foreach function to iterate over an array, resets the array pointer to the first element of the array. It is important to note that the object of the foreach operation is the copy value of the array you want to traverse, rather than traversing the array itself.

http://www.bkjia.com/PHPjc/752585.html www.bkjia.com true http://www.bkjia.com/PHPjc/752585.html techarticle recently encountered a very strange problem in the work, after using each of the functions of an array to traverse, and then pass the array as an argument to a function, inside the function again use ...

  • 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.