[PHP source code reading] array_pop and array_shift functions the previous article introduces the function for adding elements to arrays in PHP. of course, you can delete elements from arrays. Array_pop and array_shift delete only one element from the header or tail of the array. After reading the source code, we found that both functions call the same function -- _ phpi_pop to delete an array element from the array. Therefore, these two functions are explained together.
I have a more detailed description of the PHP source code on github. If you are interested, you can look around and give a star. PHP5.4 source code annotation. You can use the commit record to view the added annotations.
Function syntax
Mixed array_pop (array $ & array)
The array_pop function pops up, returns the last unit of the array, and deletes the array length by one. If array is NULL, NULL is returned.
Array_shift
Mixed array_shift (array & $ array)
Remove the elements starting with the array from the array and return the result. subtract the array length and change the number key value to 0. The text key value remains unchanged.
Sample code
The following code shows how to use array_pop and array_shift.
$arr = array(‘apple’, ‘banana’, ‘cat’);$val = array_pop($arr); // val == cat$arr = array(‘apple’, ‘banana’, ‘cat’);$val = array_shift($arr); // val == apple
Procedure
Both functions are called._ Phpi_popFunction. The difference is that the second parameter passed when the _ phpi_pop function is called.Off_the_endIf off_the_end is 1, it is array_pop; otherwise, it is array_shift. The following are the detailed steps for executing the _ phpi_pop function:
1. if the array length is 0, NULL is returned.
2. move the internal pointer to the array element to be deleted according to the off_the_end parameter.
3. set the returned value to the element pointed to by the pointer in step 2.
4. remove the first or last value from the array and reduce the length by one.
5. for the array_shift operation, you need to reset the array subscript, change the numeric subscript to count from 0, and the text key value remains unchanged; otherwise, you only need to modify the location of the next numeric index.
6. reset the array pointer.
The following flowchart describes the function execution process:
The following two figures show the changes of group elements and internal pointers when executing the code based on the preceding example:
Array_pop
Array_shift
The steps for calling this function by array_pop and array_shift are similar, except that:
1. when moving a 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 complete, the former only needs to modify the location of the next numeric index, while the latter needs to reset the array subscript.
Summary
If the implementation steps of the two functions are similar, you can use a parameter to differentiate which function is executed to reduce repeated code in the program.
The original article is limited in writing, so it is easy to learn. if there is anything wrong with the article, please let us know.
If this article is helpful to you, please click here for recommendations. thank you ^_^
At last, I have a more detailed description of the PHP source code on github. If you are interested, you can look around and give a star. PHP5.4 source code annotation. You can use the commit record to view the added annotations.
For more source code articles, visit the personal homepage to continue viewing: hoohack