Home > Article > Backend Development > [PHP source code reading] array_pop and array_shift functions, jsarraypopshift_PHP tutorial
The previous article introduced the PHP function for adding elements to an array, then of course there is the function of removing elements from the array. array_pop and array_shift only remove an element from the head or tail of the array. After reading the source code, I found that the implementation of these two functions calls the same function - _phpi_pop to implement the function of deleting an array element from the array. Therefore, these two functions are discussed together during interpretation.
I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.
<p>mixed array_pop ( array $&array )</p>
The array_pop function pops and returns the last element of the array and decrements the array length by one. Returns NULL if array is empty.
<p>mixed array_shift ( array &$array )</p>
Move the unit at the beginning of the array out of the array and return it as the result, reduce the length of the array by one and change all numeric key values to start counting from 0, and the text key values remain unchanged.
The following code shows how to use array_pop and array_shift
<span>$arr</span> = <span>array</span>(‘apple’, ‘banana’,<span> ‘cat’); </span><span>$val</span> = <span>array_pop</span>(<span>$arr</span>); <span>//</span><span> val == cat</span> <span>$arr</span> = <span>array</span>(‘apple’, ‘banana’,<span> ‘cat’); </span><span>$val</span> = <span>array_shift</span>(<span>$arr</span>); <span>//</span><span> val == apple</span>
Both functions call the _phpi_pop function. The difference is the second parameter off_the_end passed when calling the _phpi_pop function. If off_the_end is 1, then is array_pop, otherwise array_shift. The following are the detailed steps for executing the _phpi_pop function:
<p>1、如果数组长度为0,则返回NULL。</p> <p>2、根据off_the_end参数移动内部指针指向需要删除的数组元素。</p> <p>3、设置返回值为第二步指针指向的元素。</p> <p>4、从数组中移出第一个或最后一个值并将长度减一。</p> <p>5、如果是array_shift操作,则需要重置数组下标,将数字下标改为从0开始计数,文字键值不变;否则只需要修改下一个数字索引的位置。</p> <p>6、重置array指针。</p>
The process of function execution can be described by the following flow chart:
array_pop
The steps performed by array_pop and array_shift in calling this function are similar. The difference is:
1. When moving the pointer, the former moves to the end of the array, and the latter moves the pointer to the first unit of the array.
2. After the deletion operation is completed, the former only needs to modify the position of the next numerical index, while the latter needs to reset the array subscript.
Summary
Original article with limited writing style and limited knowledge. If there is anything wrong in the article, please let me know.
If this article is helpful to you, please click to recommend it, thank you^_^
Finally, I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.
For more source code articles, please visit your personal homepage to continue viewing: hoohack
http://www.bkjia.com/PHPjc/1130144.html