Simple understanding of the use of pointers to arrays in PHP programming _php tips

Source: Internet
Author: User
Tags pear php programming prev

To use the elements in an array, you need the positioning of the array. The implementation of the positioning needs to be done with an array pointer. There are many functions in PHP that can move array pointers. The following will introduce you to a few.

Move the pointer to the next array position next ()

The next () function returns the array value immediately following the next position of the current array pointer. The form is as follows:

Mixed next (array array)

Here is an example:

$fruits = Array ("Apple", "banana", "orange", "pear");

Echo Next ($fruits);

Echo Next ($fruits);

Banana

//Orange

You can also move the pointer forward or directly to the beginning and end of the array.

Move the pointer to the previous array position prev ()

The prev () function returns the array value at a position before the current pointer, or False if the pointer is already in the first position of the array. The form is as follows:

Mixed prev (array array)

The use of Prev () is the same as next (), and the example is omitted.

Move the pointer to the first array position reset ()

The reset () function sets the array pointer back to the starting position of the array. The form is as follows:

Mixed reset (array array)

This function is often used when you need to view or process an array more than once in a script, and this function is often used at the end of a sort.

Move the pointer to the last array position end ()

The end () function moves the pointer to the last position in the array, returning the last element. The form is as follows:

Mixed end (array array)

The following example shows if you get the first and last array values:

$fruits = Array ("Apple", "banana", "orange", "pear");

Echo current ($fruits);

Echo End ($fruits);

Apple

//Pear

There is another rule for passing an array as a parameter between functions: We know that when a function is called, the system assigns a copy of the argument to the formal parameter (except the reference call), and for the array, not just copy the value of the argument, but also copy the position of the current internal pointer of the real parameter group. If the position of the argument's internal pointer points to the end of the array, the system resets the internal pointer of the formal parameter. The first cell of a pointing parameter group; If the argument's internal pointer is not positioned at the end of the array, pointing to a valid cell, the system will point the array pointer position of the parameter to the array cell with the same value as the argument's array pointer.

If you do not do $arr [' var6 '] = 6 This step, 6 variables ($var 1-$var 6) will have a value, because after each, the array pointer has already pointed to the end of the array, then when the function func () is invoked, the system resets the $ARRTMP array pointer. Point it to the first element. But after the $arr [' var6 '] = 6 operation, everything changed, and this one made the $arr array pointer from the original point to a null to become a valid value (note that before and after the assignment, the array pointer points to the address cell has been unchanged, but before the assignment, That address cell has nothing, and the assignment becomes 6. This allows the $arr array pointer to point to a valid cell, and when the function func () is invoked, 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. and the each function starts at 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, which moves the array pointer down one bit, and the while 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 assigned array pointer changes. Let's start with a conclusion, and then we'll use the code to prove the conclusion. $arrtmp = $arr; In this assignment expression, I call $arr an array of assignments, and $arrtmp is called an array of assigned values. If an array pointer to an array of assignments already points to the end of an array when it is assigned a value, the array pointer to the assigned array after the assignment is reset to point to the first element of the array, and if the array pointer to the assignment array does not point to the end of the array when the assignment is made, it points to any valid array element. The array pointer to the assigned array after the assignment is not reset, but rather retains the element it originally pointed to. After the assignment, the assigned array not only has the value of the assignment array, but the array pointer to the assignment array points to that element, and the assigned array points to the same element with the same value in it.

Demo1:

<?php
 $arr = Array (' var1 ' =>1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5);
 while (the 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));
? >

The results of the demo1 are: Int (5) int (5) Int (5). From this result, we can see that there is no change in the position of the $arr array pointer before and after the assignment, $arr 1 not only has the same value as $arr, but also the element value that the array pointer points to is the same. Now using the above conclusion to explain this result, in the while loop, there is an if judgment statement, which is designed to keep the $arr array pointer to the end of the array, but to remain in a valid position. The $value=4 jumps out of the loop, and each function moves the array pointer forward one bit, which causes the $arr array pointer to point to the 5th element, so the result of current ($arr) is 5 before the assignment, because before the assignment, the $ The current pointer to the ARR does not point to the end, so the $arr array pointer is not reset after the assignment, but its original position is preserved, so the result of using current ($arr) after the assignment is still 5. When assigned, $ARR1 not only obtains the $arr value, but the array pointer points to the same element as the $arr, both of which are 5.

<?php
$arr = Array (' var1 ' =>1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5);
while (the 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 break the if ($value = = 4); This comment is dropped, and the purpose is simply to point the $arr array pointer position to the end of the array.

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, $arr value is not 0 or "", so it can be concluded that the array pointer is pointing to an invalid element and that current returns a false. In other words, it is possible to determine that after the while loop completes, the $arr array pointer has already pointed to the end of the array. So we see that the value of current ($arr) before the assignment is false, and the value of current ($arr) after the assignment becomes 1, indicating that the $arr array pointer was reset after the assignment, pointing to the first element of the array. The value of current ($arr 1) is false, which indicates the element to which the $arr array pointer is $arr1 after the assignment, after which the value is left.

This conclusion can be proved by Demo1 and Demo2.

So in order to traverse an array without being affected by array pointers, it is a good idea to reset the array pointer before or after 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 acts on the array pointer, which is to take the current position of the array pointer back one bit, and it also needs to be noted that if the array pointer is already pointing at the end of the array, it will not get the desired result.

By the way, foreach this function, when you use the Foreach function to traverse 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.

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.