Introduction: This is a detailed page for PHP array pointer exploration. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 338288 'scrolling = 'no'> recently, I encountered a very strange problem in my work. After using the each function to traverse an array, pass the array as a real parameter to a function, and use each to traverse the array of form parameters again within the function. The purpose of using the each function twice is to convert the key in the array to the variable name, and the value corresponding to the key to the variable value. In fact, this function can be implemented using the extract function. Next, let's talk about what problems occurred when using the each function inside the function. After traversing, we found that some variables are null, that is, some variables are lost. I do not know whether the description is clear or I want to simplify the problem at work.CodeLet's explain it.
<? PHP
$ Arr = array ('var1' => 1, 'var2' => 2, 'var3' => 3, 'var4' => 4, 'var5' => 5 );
While (List ($ key, $ value) = each ($ ARR ))
{
$ Key = $ value;
}
// The next step is critical. The problem lies here. After traversing, add a value to the array.
$ Arr ['var6'] = 6;
Func ($ ARR );
Function func ($ arrtmp)
{
While (List ($ key, $ value) = each ($ arrtmp ))
{
$ Key = $ value;
}
Var_dump ($ var1 );
Var_dump ($ var2 );
Var_dump ($ var3 );
Var_dump ($ var4 );
Var_dump ($ var5 );
Var_dump ($ var6 );
}
?>
Output: NULL null int (6 ).
According to the general idea, the $ var1, $ var2, $ var3, $ var4, $ var5, and $ var6 variables in the function func should all have them, but this is not the case, only the variable $ var6 has a value, while the other variables are null. Why?
The problem lies in the array pointer that we will discuss today. The each function returns the elements pointed to by the current array pointer in the form of an array, and moves the array pointer one forward to the next array unit. After we use the each function to traverse the array $ arr, the internal pointer of the $ arr array has pointed to the next bit of the last unit (with no value ). At this time, we executed the $ arr ['var6'] = 6 operation and added a new unit to the array. We know that, arrays stored in the memory must be sequential address units. That is to say, the position of $ arr ['var6'] in the memory should be the Unit pointed to by the current array pointer (previously empty ). In addition, assigning values to an array does not move the internal pointer of the array. After assigning values, the $ arr array pointer is changed from pointing to a null to pointing to an address unit with an actual value.
There is another rule for passing an array as a parameter between functions: we know that when a function is called, the system will assign a copy of the real parameter to the form parameter (except for reference calls ), for arrays, we not only copy the values of real parameters, but also copy the position of the current internal pointer of the real parameter array. If the internal pointer of the real parameter points to the end of the array, the system resets the internal pointer of the parameter to the first unit of the parameter group. If the internal pointer of the real parameter is not at the end of the array, that is, it points to a valid unit. Then, the system points the array pointer position of the form parameter to an array unit with the same value as the array pointer of the real parameter.
If you do not perform the $ arr ['var6'] = 6 operation, all the six variables ($ var1-$ var6) have a value, because after each, the array pointer has already pointed to the end of the array. When the func () function is called, the system resets the $ arrtmp array pointer and points it to the first element. But after $ arr ['var6'] = 6, everything changes, this operation changes the $ arr array pointer from pointing to a null value to a valid value (Note: Before and After the value assignment, the address that the array Pointer Points to is unchanged, but before the value assignment, there is no address unit, and after the value assignment is changed to 6 ). This causes the $ arr array pointer to point to a valid unit. When the func () function is called, the system will not reset the $ arrtmp array pointer, the $ arrtmp array pointer will be the same as the $ arr array pointer, pointing to his own last unit. The each function starts from the position of the current array pointer. Therefore, the return value of the first result of the each function operation is the last element of the array $ arrtmp. It moves the array pointer down and the while loop ends here, therefore, $ arrtmp ['var1']-$ arrtmp ['var5'] is not traversed, resulting in $ var1-$ var6 being null.
Tomorrow, we will continue to analyze the changes in the array pointer of the assigned array and the assigned array.
The above is just some of my current understanding of array pointer calls between functions. There may be a lot of errors. I hope you can give me some advice!
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/338288.html pageno: 8.