


[PHP source code reading]array_push and array_unshift functions, arrayunshift_PHP tutorial
[PHP source code reading]array_push and array_unshift functions, arrayunshift
In PHP, adding elements to an array is also a very common operation, at the end and head of the array respectively. Add elements from the bottom to see how PHP implements array insertion operations internally.
I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.
array_push
<p>int array_push ( array &$array , mixed $value1 [ , mixed $... ] )</p>
The array_push function treats the array parameter as a stack and pushes the passed variables to the end of the array. The length of the array increases with the number of variables pushed in. The following code has meaningful effects:
$array[] = $var;
If you only need to add one element to the array, it is better to use $array[] because it does not require calling a function.
Run the example
<span>$arr</span> = <span>array</span><span>(); </span><span>array_push</span>(<span>$arr</span>, 1, 2, 3); <span>//</span><span> return 3; $arr = [1, 2, 3]</span>
Run steps
The array_push function is relatively simple. It is equivalent to a push operation. Treat the array as a stack, then turn each parameter into a reference, add one to the reference number, and then add it to the end of the array.
The internal implementation flow chart is as follows:
$arr = array(1, 2, 3);
array_unshift($arr, 4, 5, 6); // 4 5 6 1 2 3
Run steps
<p>1、调用php_splice将数据元素插入到数组头部,用新的哈希表替换就得哈希表并将其销毁</p> <p>2、如果操作后的stack等于运行时的符号表,则重置哈希表的内部指针</p> <p>3、stack指向新的哈希表,释放新的哈希表红箭,销毁就得哈希表</p>
Source code interpretation
As can be seen from the above steps, the core step of array_unshift is the php_splice function. For the array_unshift function, php_splice creates a new hash table out_hash during implementation, inserts the list data to be inserted into out_hash first, and then writes the original array data into out_hash, thus realizing the function of inserting data elements in front of the array.
The realized renderings are as follows:
In the process of reading the source code this time, I also studied the hash table data structure and some APIs in PHP, and also supplemented myself with some knowledge of hash tables. I learned that the bottom layer of PHP uses a two-way linked list to handle hash conflicts, and I benefited a lot. I will share more about PHP data structure in the future.
Original article with limited writing style and limited knowledge. If there is anything wrong in the article, please let me know.
If this article is helpful to you, please click to recommend it, thank you^_^
Finally, I have more detailed annotations on the PHP source code on github. If you are interested, you can take a look and give it a star. PHP5.4 source code annotations. You can view the added annotations through the commit record.
For more source code articles, please visit your personal homepage to continue viewing: hoohack

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Solution to the problem that pressing shift cannot switch between Chinese and English: 1. Find the win key and click to open; 2. Find "Settings" and click to open, then open "Time and Language"; 3. Click "Region and Language" and open "Options" "; 4. Click "Options" and then select "shift" in the first line.

The shift key of Mac is a key above the fn key and below the caps lock key. This key is on the far left side of the keyboard, the second key from the bottom right; the shift key is the upshift key in the keyboard, and can be accessed through " Ctrl+Shift" key combination to switch input methods.

ctrl+shift is a combination shortcut key for switching input methods; when pressing "ctrl+shift" at the same time, you can also hold down other keys to achieve other functions, such as pressing "ctrl+shift+N" at the same time key to create a new folder, etc.

Recently, some friends have reported that they don’t know where to press, causing the shift key on the keyboard to be locked. What’s going on? How to solve this situation? Here I will give you a detailed introduction to Win10 You can take a look at how to unlock the keyboard shift lock. Undoing method: 1. Click "Start" in the taskbar in the lower left corner and select "Settings" in the menu list. 2. After entering the new interface, click the "Time and Language" option. 3. Then click "Region and Language" in the left column, then click "Advanced Keyboard Settings" in "Related Settings" on the right 4. Then click "Language Bar Options". 5. In the window that opens, click the "Advanced Key Settings" tab at the top. 6. Finally

Shift is a shift key in the keyboard, also called the keyboard shift key, located in the lower left corner of the keyboard; the shift key has functions such as switching input methods, quickly switching between half-width and full-width, selecting consecutive files, and directly deleting files.

This article introduces how to easily enter safe mode in Win11 system by using the shortcut key Shift+right-click on the desktop. Safe mode is a special startup mode used to diagnose and solve computer problems. It provides a way to repair the system when it encounters a fault or fails to start normally. Through the guidance of this article, readers can learn how to use this shortcut to quickly enter safe mode in Win11 to better maintain and repair the computer system. 1. Click [right-click] on a blank space on the desktop, and in the menu item that opens, select [New - Text Document]; 2. Create a new text document window and enter the following code: WindowsRegistryEditorVersion5.00[HKEY_CL

Microsoft today released the Win11Build23419 preview update for WindowsInsider project members in the Dev channel. In addition to many improvements such as the new USB4 settings page in this update, some new functions/features introduced by Microsoft have not been written into the update log. Microsoft is about to introduce a new function/feature to Win11. Users can use the Win+Shift+R shortcut combination to record the screen. However, this function is not yet effective. After using this keyboard shortcut, the regular screenshot UI will be opened instead of the screen recording UI. Netizens can enable the above new functions and features through the ViveTool tool: 1. Download the Vivetool tool from the GitHub page, and then

Protecting computers from "shift backdoor" attacks requires a combination of security measures, including updating systems, using strong passwords and multi-factor authentication, security education, firewalls and security software, regular backups of data, network security audits and monitoring, and security Policy and permission management. Through these measures, you can significantly reduce your system's risk of "shift backdoor" attacks and ensure the security of your computer system.
