[PHP source code] array_push and array_unshift functions, arrayunshift. [PHP source code reading] array_push and array_unshift functions, arrayunshift in PHP, adding elements to the array is also a very common operation, including adding elements to the end of the array and the header, respectively, [PHP source code reading] array_push and array_unshift functions, arrayunshift
In PHP, adding elements to an array is also a common operation. you can add elements to the end and header of the array to see how PHP inserts an array.
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.
Array_push
int array_push ( array &$array , mixed $value1 [ , mixed $... ] )
The array_push function regards the array parameter as a stack and overwrites the passed variable to the end of the array. The length of array increases with the number of variables that are pressed in. The following code has a meaningful effect:
$ Array [] = $ var;
If you only need to add an element to the array, it is better to use $ array [], because you do not need to call a function in this way.
Running example
$arr = array();array_push($arr, 1, 2, 3); // return 3; $arr = [1, 2, 3]
Procedure
The array_push function is relatively simple, which is equivalent to a stack operation. the array is regarded as a stack, and each parameter is converted into a reference, the number of references plus one is referenced, and then added to the end of the array.
The internal implementation flowchart is as follows:
$ Arr = array (1, 2, 3); array_unshift ($ arr, 4, 5, 6); // 4 5 6 1 2 3Procedure
1. call php_splice to insert the data element to the array header. replace the data element with a new hash table and destroy it.
2. if the operated stack is equal to the symbol table at run time, the internal pointer of the hash table will be reset.
3. stack points to the new hash table, releases the new hash table, and destroys the hash table.
Source code explanation
The preceding steps show that the core steps of array_unshift arePhp_spliceFunction. For the array_unshift function, a new hash table out_hash is created when php_splice is implemented. the list data to be inserted is first inserted into out_hash, and then the original array data is written into out_hash, in this way, the data element is inserted before the array.
The implementation is as follows:
During this reading of the source code, I also studied the hash table data structure and some APIs in PHP, and added some knowledge about hash tables. I learned that the underlying layer of PHP is to use a two-way linked list for hash conflict processing, which has benefited a lot. We will share the PHP Data structure later.
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
Adding an element to an array in PHP is also a common operation. you can add an element to the end and header of the array, respectively ,...