[PHP source reading]array_push and Array_unshift functions, arrayunshift_php Tutorial

Source: Internet
Author: User

[PHP source reading]array_push and Array_unshift functions, Arrayunshift


In PHP, adding elements to an array is also a very common operation, including adding elements at the end of the array and the head, to see how the inside of PHP implements the operation of array insertions.

I have a more detailed comment on the PHP source code 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.

Array_push

int Array_push (array & $array, mixed $value 1 [, Mixed $ ...]

The Array_push function sees the array parameter as a stack, which overrides the trailing of the array by passing in the variable. The length of the array increases with the number of variables being pressed in. The following code has a meaningful effect:

$array[] = $var;

If you only need to add an element to an array, use $array[] this way is better, because doing so does not call the function.

Run the sample

$arr Array (); Array_push ($arr//  return 3; $arr = [1, 2, 3]

Run steps

The Array_push function is relatively simple, it is equivalent to the stack operation, the array as a stack, and then for each parameter, let it become a reference, the reference number plus one, and then add it 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 3

Run steps

1. Call Php_splice to insert the data element into the array header, replace it with a new hash table and destroy it.

2. Reset the internal pointer of the Hashtable if the stack equals the run-time symbol table

3. Stack points to the new hash table, releases the new hash table Red Arrows, destroys the hash table

SOURCE Interpretation

From the above steps, the core step of Array_unshift is the php_splice function. For the Array_unshift function, a new hash table Out_hash is created when the Php_splice is implemented, the list data that needs to be inserted is first inserted into the Out_hash, and then the original array data is written to the Out_hash. This implements the ability to insert data elements before the array.

The implementation is as follows:


In the process of reading the source code, we also studied the hash table data structure and some APIs in PHP, and also added some knowledge of hash table. learned that PHP bottom is the use of doubly linked list to do hash conflict processing, benefited greatly. Share the PHP data structure later.

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/1129452.html www.bkjia.com true http://www.bkjia.com/PHPjc/1129452.html techarticle [php source reading]array_push and Array_unshift functions, arrayunshift in PHP, adding elements in the array is also a very common operation, respectively, in the end of the array and the head to add elements, ...

  • 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.