Home  >  Article  >  Backend Development  >  A brief discussion of PHP source code nine: introduction to array_unshift, array_push

A brief discussion of PHP source code nine: introduction to array_unshift, array_push

不言
不言Original
2018-06-28 16:58:532290browse

This article mainly introduces a brief discussion of PHP source code nine: an introduction to array_unshift, array_push, which has a certain reference value. Now I share it with you. Friends in need can refer to it.

It’s Chinese New Year today. , I wish all my friends a happy New Year and all the best!

A brief talk about PHP source code nine: introduction to array_unshift, array_push

int array_unshift (array &array, mixed var [, mixed ...])

array_unshift() will The passed in cell is inserted at the beginning of the array array. Note that cells are inserted as a whole, so incoming cells will remain in the same order. All numeric key names will be modified to start counting again from zero, and all text key names will remain unchanged.

Returns the new number of cells in the array array.

In line 2080 of standard/array.c, you can see the C implementation of this function PHP_FUNCTION(array_unshift)

The program will first determine whether the number of input parameters is correct. If it is less than 2, Report an error
Then determine whether the first parameter is an array. If not, report an error and exit
Then the program will call new_hash = php_splice(Z_ARRVAL_P(stack), 0, 0, &args[1], argc-1, NULL) ;

HashTable* php_splice(HashTable *in_hash, int offset, int length, zval ***list, int list_count, HashTable **removed)
This function is in line 1861 of array.c
It will first copy part of in_hash (original hashtable) to the new hashtable. This part is calculated based on the offset and length given. The offset and length given by array_unshif are both 0, so no elements are copied to the new hashtable. The hashtable
then traverses the list, creates a zval for each element and inserts it into the new hashtable
using zend_hash_next_index_insert, and then copies the remaining elements in in_hash to the new hashtable. Here, since offset and length are both 0 , so it is all hashtable
Finally, return the newly generated hashtable
The whole process is equivalent to writing the data in the list to the hashtable first, and then writing the old data to the hashtable, thus realizing the front of the array Insert elements

Then delete the hashtable where the old array is located and refresh the new HashTable, and reset the internal pointer of the hashtable,
Return the number of elements in the hashtable (that is, the length of the newly generated array)

int array_push (array &array, mixed var [, mixed ...] )

array_push() treats array as a stack and pushes the passed variables into the end of array. The length of array will increase according to the number of variables pushed onto the stack.
The same effect as the following:

   <?php$array[] = $var;?>

And repeat the above action for each var.

Return the total number of new cells in the array.

This implementation is relatively simple:
Directly traverse the given parameters, create a zval for each element, add one to its reference, and add it to the end of the hashtable where the array is located.
Return the number of elements in the hashtable (that is, the length of the newly generated array)

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website !

Related recommendations:

A brief discussion on PHP source code 8: Introduction to array_pop, array_shift

A brief discussion on PHP source code 7: About nl2br , ltrim, rtrim, trim function

A brief discussion on PHP source code 6: About the stream_get_wrappers function

The above is the detailed content of A brief discussion of PHP source code nine: introduction to array_unshift, array_push. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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