[PHP源码阅读]array_push和array_unshift函数,arrayunshift
在PHP中,在数组中添加元素也是一种很常用的操作,分别有在数组尾部和头部添加元素,看看PHP内部是如何实现数组插入的操作。
我在github有对PHP源码更详细的注解。感兴趣的可以围观一下,给个star。PHP5.4源码注解。可以通过commit记录查看已添加的注解。
array_push
<p>int array_push ( array &$array , mixed $value1 [ , mixed $... ] )</p>
array_push函数将array参数看做一个栈,将传递进来的变量压倒array的尾部。array的长度随着被压进去的变量个数增加。下面的代码有意义的效果:
$array[] = $var;
如果只需要添加一个元素到数组,使用$array[] 这种方式更好,因为这样做不用调用函数。
运行示例
<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>
运行步骤
array_push函数相对比较简单,就相当于压栈操作,把array看做一个栈,然后对每一个参数,让其变成引用,引用数加一,然后添加它到数组的尾部。
内部实现的流程图如下:
$arr = array(1, 2, 3); array_unshift($arr, 4, 5, 6); // 4 5 6 1 2 3
运行步骤
<p>1、调用php_splice将数据元素插入到数组头部,用新的哈希表替换就得哈希表并将其销毁</p> <p>2、如果操作后的stack等于运行时的符号表,则重置哈希表的内部指针</p> <p>3、stack指向新的哈希表,释放新的哈希表红箭,销毁就得哈希表</p>
源码解读
由上面的步骤可知,array_unshift的核心步骤是php_splice函数。对于array_unshift函数,php_splice实现时新建一个哈希表out_hash,将需要插入的list数据先插入到out_hash中,然后再把原来的数组数据写入到out_hash中,这样实现在数组前面插入数据元素的功能。
实现的效果图如下:
这次阅读源码过程中,同时也研究了PHP中的哈希表数据结构及一些API,也给自己补充了一些哈希表的知识。学习到了PHP底层是使用双向链表做哈希冲突的处理,获益匪浅。日后再做关于PHP数据结构的分享。
原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。
如果本文对你有帮助,请点下推荐吧,谢谢^_^
最后再安利一下,我在github有对PHP源码更详细的注解。感兴趣的可以围观一下,给个star。PHP5.4源码注解。可以通过commit记录查看已添加的注解。
更多源码文章,欢迎访问个人主页继续查看: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

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
