[PHP source reading]array_pop and Array_shift functions, Jsarraypopshift
The previous article introduced PHP to add elements to an array of functions, then of course there are elements removed from the array. Array_pop and Array_shift only remove an element from the head or tail of an array. After reading the source code, it is found that both functions are implemented by invoking the same function--_phpi_pop to implement the function of removing an array element from the array. So the two functions are explained in conjunction with the interpretation.
I have a more detailed comment on the PHP source on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.
function syntax
Array_pop
Mixed Array_pop (array $&array)
The Array_pop function pops up and returns the last cell of the array, reducing the length of the array by one. Returns null if array is empty.
Array_shift
Mixed Array_shift (array & $array)
Move the cells at the beginning of the array to the group and return as a result, and subtract the array length from all numeric key values to start counting from 0, with the text key value unchanged.
code example
The following code shows how Array_pop and Array_shift are used
$arr Array (' Apple ', ' banana ', ' cat '); $val Array_pop ($arr// val = = Cat$arrarray(' Apple ', ' banana ', ' cat '); $val Array_shift ($arr// val = = Apple
Perform steps
Two functions are called the _phpi_pop function, the difference is to call the _phpi_pop function when the second parameter passed off_the_end difference, if the off_the_end is 1, then Array_ Pop, otherwise it is array_shift. Here are the detailed steps _phpi_pop this function performs:
1, if the array length is 0, returns NULL.
2. Move the internal pointer according to the Off_the_end parameter to point to the array element that needs to be deleted.
3. Set the return value to the element that the second step pointer points to.
4. Remove the first or last value from the array and subtract the length by one.
5, if the array_shift operation, you need to reset the array subscript, the number subscript to start counting from 0, the text key value is not changed, otherwise only need to modify the position of the next numeric index.
6. Reset the array pointer.
The procedure for performing a function can be described in the following flowchart:
Array_pop
Array_shift
Array_pop and Array_shift Call this function to perform a similar procedure, except that:
1. When moving the pointer, the former moves to the tail of the array, which moves the pointer to the first cell of the array.
2. Once the delete operation is complete, the former simply modifies the position of the next numeric index, which needs to reset the array subscript.
Summary
If the two functions implement the same steps, you can use a parameter to distinguish which function is executing to reduce the code duplication in the program.
Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.
If this article is helpful to you, please click on the recommendation, thank you ^_^
Finally Amway again, I have a more detailed comment on the PHP source code in GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation. Comments that have been added can be viewed through a commit record.
More source articles, welcome to personal homepage Continue to view: Hoohack
http://www.bkjia.com/PHPjc/1130144.html www.bkjia.com true http://www.bkjia.com/PHPjc/1130144.html techarticle [php source reading]array_pop and Array_shift functions, jsarraypopshift the previous article introduced the PHP add elements to the function of the array, then of course there are elements removed from the array. Array_pop and Arra ...