PHP array pointer function

Time:2020-2-26

Array pointer functions include reset(), prev(), current(), next(), end(), key(), each()

Among them, reset(), prev(), current(), next(), end() are all functions only related to the value of the array, key() only related to the key of the array, and each() can obtain the value and key of the array

Reset() function. The parameter is an array. It refers to the value passed. It resets the internal pointer of an array to the first place, that is, the location of the first element of the array, and then returns the value of the first element. If the array is empty, it returns false

 $arr = [ 1, 2 ];
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(1)
 // bool(false)

End() function. The parameter is an array. It refers to the value passed. It moves the internal pointer of an array to the location of the last element of the array, and then returns the value of the last element. If the array is empty, it returns false

 $arr = [ 1, 2 ];
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(2)
 // bool(false)

The current() function, whose parameter is an array, refers to the passed value and returns the value pointed by the current array pointer. If the pointer moves beyond the end of the array, it returns false. If the array is empty, it returns false

Array pointer defaults to the first bit of the array

 $arr = [ 1, 2 ];
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(1)
 // bool(false)

The next() function, whose parameter is an array, refers to the passed value, moves the array pointer to the next bit, and returns the value of this bit. When there is no element in this bit, it returns false, and the empty array also returns false

 $arr = [ 1, 2 ];
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(2)
 // bool(false)

Prev() function. The parameter is an array. It refers to the value passed, moves the array pointer to the previous bit, and returns the value of this bit. When there is no element in this bit, it returns false, and null array also returns false

 $arr = [ 1, 2 ];
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(2)
 // bool(false)

The key() function, whose parameter is an array, refers to the value passed, and returns the key of the element pointed to by the current pointer. When there is no element pointed to by the pointer, null is returned by the null array

 $arr = [ 1, 2 ];
 ( $arr );
 ( ( $arr ) );
 $arr = [ ];
 ( ( $arr ) );
 // int(2)
 // bool(false)

Each() function, the parameter is an array, references the passed value, returns the value and key of the element that the current pointer refers to, and moves the pointer to the next bit

This function is extremely discouraged after php7.2

Warning

This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged

 

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: https://learnku.com/laravel/t To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]