Home  >  Article  >  Backend Development  >  A brief discussion of PHP source code twenty-five: about next, current, key functions

A brief discussion of PHP source code twenty-five: about next, current, key functions

不言
不言Original
2018-06-28 17:59:021671browse

This article mainly introduces about PHP source code twenty-five: about next, current, key function, it has certain reference value. Now I share it with everyone. Friends in need can refer to it

A brief discussion on PHP source code twenty-five: About next, current, key function

key — Get the key name from the associative array
mixed key (array &array)
key() returns the current unit in the array key name.

This function returns the key value by calling zend_hash_get_current_key_ex in zend_hash.c
In the zend_hash_get_current_key_ex function, determine whether the key is a string or a number based on the nKeyLength attribute, and then return

current — Returns the current unit in the array
mixed current (array &array)
Each array has an internal pointer pointing to its "current" unit, initially pointing to the first unit inserted into the array.
The current() function returns the value of the array unit currently pointed to by the internal pointer and does not move the pointer. If the internal pointer points beyond the end of the cell list, current() returns FALSE.

This function returns value by ultimately calling the zend_hash_get_current_data_ex function
zend_hash_get_current_data_ex function directly returns the value stored in the array element:
*pData = p->pData;
If in the array If there is a false element, the return value is the same as the return value if it is not found. This is a confusing place.

next — Move the internal pointer in the array forward one bit
mixed next (array &array)
Returns the value of the next cell pointed to by the array's internal pointer, or returns FALSE when there are no more cells.

next() and current() operations are very similar, with one difference: the internal pointer is moved forward one bit before returning the value. That is, zend_hash_move_forward(target_hash);
This means that it returns the value of the next array unit and moves the array pointer forward one bit. If moving the pointer results in moving the pointer beyond the end of the array element, next() returns FALSE.
And like current(), when false exists in the array element, the return value of next() will also be false

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A brief discussion on PHP source code twenty-four: Analysis of the reasons why iteration cannot be completed when the value is false in iterator implementation

A brief discussion on PHP source code twenty-three: About the urlencode function

The above is the detailed content of A brief discussion of PHP source code twenty-five: about next, current, key functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn