Home > Article > Backend Development > Briefly understand the use of array pointers in PHP programming_php skills
To use the elements in the array, you need the positioning of the array. The implementation of positioning needs to be completed with the help of array pointers. There are many functions in PHP to move array pointers. I will introduce a few to you below.
Move the pointer to the next array position next()
The next() function returns the array value placed immediately next to the current array pointer. Its 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 move it to the beginning and end of the array.
Move the pointer to the previous array position prev()
The prev() function returns the array value located at the previous position of the current pointer. If the pointer is originally located at the first position of the array, it returns false. Its form is as follows:
mixed prev(array array)
The usage of prev() is the same as next(), so the example is omitted.
Move the pointer to the first array position reset()
The reset() function is used to set the array pointer back to the beginning of the array. Its form is as follows:
mixed reset(array array)
This function is often used if you need to view or process an array multiple times in a script. In addition, this function is often used at the end of sorting.
Move the pointer to the last array position end()
Theend() function moves the pointer to the last position of the array and returns the last element. Its form is as follows:
mixed end(array array)
The following example shows how to get the first and last array value:
$fruits = array("apple", "banana", "orange", "pear"); echo current($fruits); echo end($fruits); // apple // pear
There is such a rule when arrays are passed as parameters between functions: we know that when a function is called, the system will copy a copy of the actual parameters to the formal parameters (except for reference calls), but for arrays, not only copy The value of the actual parameter is obtained, and the position of the current internal pointer of the actual parameter group is also copied. If the position of the internal pointer of the actual parameter points to the end of the array, the system will reset the internal pointer of the formal parameter to point to the first unit of the formal parameter group; if the position of the internal pointer of the actual parameter is not at the end of the array, it points to a valid unit, then the system will point the array pointer position of the formal parameter to the array unit with the same value as the array pointer of the actual parameter.
If you do not do $arr['var6'] = 6, all 6 variables ($var1-$var6) will have values, because after each, the array pointer has pointed to the end of the array, then after When the function func() is called, the system resets the array pointer of $arrtmp to point to the first element. But after the operation $arr['var6'] = 6, everything changed. This operation changed the array pointer of $arr from pointing to a NULL to a valid value (explain, before and after the assignment, the array The address unit pointed by the pointer has not changed. It is just that before the assignment, there was nothing in that address unit, but after the assignment, it became 6). This makes the array pointer of $arr point to a valid unit. When calling function func(), the system will not reset the array pointer of $arrtmp. The array pointer of $arrtmp will be the same as the array pointer of $arr. Same, pointing to his own last unit. The each function starts working 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, which moves the array pointer down one more bit. The while loop ends here, so $arrtmp['var1']-$arrtmp ['var5'] has not been traversed, eventually causing $var1-$var6 to be NULL.
In the process of array assignment, the changes in the array pointers of the assigning array and the assigned array. First give a conclusion, and then we use code to prove this conclusion. $arrtmp=$arr; In this assignment expression, I call $arr the assignment array, and $arrtmp the assigned array. When the array is assigned, if the array pointer of the assigned array already points to the end of the array, the array pointer of the assigned array will be reset after the assignment and points to the first element of the array; if during the assignment, the array pointer of the assigned array does not point to the array. At the end, it points to any valid array element. Then the array pointer of the assigned array will not be reset after the assignment, but the element it originally pointed to will be retained. After the 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, and the assigned array will also point to the element with the same value in itself.
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)); ?>
demo1 的执行结果是:int(5) int(5) int(5) 。从这个结果可以看出,赋值前后$arr的数组指针位置没有发生任何变化,$arr1不仅值跟$arr相同,而且数组指针所指向的元素值也是相同的。现在 用上述结论来解释这个结果,在while循环中,有一个if判断语句,目的是不让$arr的数组指针指向数组末尾,而是保留在一个有效的位置。 在$value=4时会跳出循环,而each这个函数会将数组指针向前移动一位,这就导致了$arr的数组指针指向了第5个元素,所以在赋值之 前,current($arr)的结果是5,赋值之后,由于在赋值之前$arr的当前指针并没有指向末尾,因此在赋值之后不会将$arr的数组指针进行重 置,而是保留了其原有的位置,因此在赋值之后使用current($arr)的结果仍然是5。赋值时$arr1不仅获得了$arr的值,而且数组指针指向 的元素和$arr的相同,二者都是5。
<?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)); ?>
demo2中我们将 if($value == 4) break; 这一句注释掉了,目的很简单,就是通过each将$arr的数组指针位置指向数组末尾。
demo2 的执行结果:bool(false) int(1) bool(false) 。如果数组指针对应的元素为0,"",或者不是一个有效的值时,current函数会返回false,$arr的值中没有为0或者""的情况,因此可以断 定是因为数组指针指向了一个无效的元素而导致current返回了一个false。换句话说就是可以确定在while循环完成之后,$arr的数组指针已 经指向了数组的末尾。所以我们看到在赋值之前current($arr)的值是false,而赋值之后current($arr)的值变成了1,说明赋值 之后$arr的数组指针被重置了,指向了数组的第一个元素。current($arr1)的值为false,说明赋值之后$arr1让然保留了赋值之 前$arr的数组指针指向的元素。
通过demo1和demo2就可以证明上述结论了。
因此为了在遍历数组时不受数组指针的影响,最好在使用each()函数之前或者之后调用函数reset()将数组指针重置。这样就可以避免上述问题的发生了。另外还有一个操作数组指针的函数prev(),它的作用是将数组指针当前的位置后退一位,它也需要注意一点,就是如果数组指针已经指向数组末尾,那么使它就得不到想要的结果了。
顺便说一下foreach这个函数,使用foreach函数来遍历数组时,它会重置数组指针,将其指向数组的第一个元素。必须注意的是foreach操作的对象是对你要遍历的数组的copy值,而不是遍历数组本身。