


A brief discussion of PHP source code nine: introduction to array_unshift, array_push
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!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
