Talking about PHP source nine: About Array_unshift, Array_push introduction

Source: Internet
Author: User
This article mainly introduces about the PHP source nine: About Array_unshift, Array_push Introduction, has a certain reference value, now share to everyone, there is a need for friends can refer to

Today's New Year, I wish you all Friends Happy Spring Festival, good luck!

Talking about PHP source nine: About Array_unshift, Array_push introduction

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

Array_unshift () Inserts the incoming cell at the beginning of the array. Note that the units are inserted as a whole, so incoming cells will remain in the same order. All numeric key names are modified to count back from zero, and all text key names remain unchanged.

Returns the number of new cells in array arrays.

In the Standard/array.c 2080 line, 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 less than 2 error
Then determine whether the first parameter is an array, and if not, the error exits
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 the In_hash (the original Hashtable) part of the new Hashtable, here is a part of the offset and length according to the calculation, Array_unshif gave the offset and length of 0, So here's not copying any of the elements to the new Hashtable
Then iterate through the list, creating a zval for each element and inserting it into the new Hashtable using Zend_hash_next_index_insert
The remaining elements in the In_hash are then copied to the new Hashtable, and we have all the hashtable here because of the offset and length of 0.
Finally, the newly generated hashtable is returned
The whole process is equivalent to writing the data in the list to Hashtable and then writing the old data to Hashtable, so that the element is inserted before the array

Then delete the hashtable of the old array and refresh the new Hashtable, and reset the hashtable internal pointer,
Returns 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 the array as a stack and presses the incoming variable into the end of the array. The length of the array will increase depending on the number of variables that are in the stack.
The effect is the same as the following:

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

and repeat the above actions for each var.

Returns the total number of new cells in the array.

This implementation is relatively straightforward:
Directly iterate over the given parameters, create a zval for each element, and add a reference to the hashtable of the array.
Returns the number of elements in the Hashtable (that is, the length of the newly generated array)

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.