[PHP source code reading] array_pop and array_shift functions, jsarraypopshift_PHP tutorial

WBOY
Release: 2016-07-12 08:51:30
Original
803 people have browsed it

[PHP source code reading] array_pop and array_shift functions, jsarraypopshift

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.

Function syntax

array_pop

<p>mixed array_pop ( array $&array )</p>
Copy after login

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.

array_shift

<p>mixed array_shift ( array &$array )</p>
Copy after login

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.

Code Example

The following code shows how to use array_pop and array_shift

<span>$arr</span> = <span>array</span>(&lsquo;apple&rsquo;, &lsquo;banana&rsquo;,<span> &lsquo;cat&rsquo;);
</span><span>$val</span> = <span>array_pop</span>(<span>$arr</span>); <span>//</span><span> val == cat</span>
<span>$arr</span> = <span>array</span>(&lsquo;apple&rsquo;, &lsquo;banana&rsquo;,<span> &lsquo;cat&rsquo;);
</span><span>$val</span> = <span>array_shift</span>(<span>$arr</span>); <span>//</span><span> val == apple</span>
Copy after login

Execution steps

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>
Copy after login

The process of function execution can be described by the following flow chart:

[PHP source code reading] array_pop and array_shift functions, jsarraypopshift_PHP tutorialarray_pop

                                                                                              [PHP source code reading] array_pop and array_shift functions, jsarraypopshift_PHP tutorial

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

If the steps implemented by two functions are similar, you can use a parameter to distinguish which function is executed to reduce repeated code in the program.

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

truehttp: //www.bkjia.com/PHPjc/1130144.htmlTechArticle[PHP source code reading] array_pop and array_shift functions, jsarraypopshift The previous article introduced the PHP function for adding elements to an array, Then of course there is removing elements from array. array_pop and arra...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!