关于PHP中数组模型的一点猜想(by misko lee)
根据我个人的理解,数组在内存中应该是一片连续的空间。在数组使用前需要在内存中开辟一片规定大小的空白内存,如果声明的大小小于可用的连续内存大小,应该报内存溢出的错误。
对于使用过PHP数组的童鞋应该能够很快速的判断出PHP中的数组并不具备以上特点。一,数组声明无需进行size的确定;二,数组没有使用上限。通过这两点,我们可以判断出,PHP中的数组是链表式的松散结构,而不是连续内存空间。
PHP中的数组可以分为索引数组以及关联数组两种形式。索引数组是一种计数型的数组结构。而关联数组则是map数据结构的一种实现,也就是key-value的结构。
我们通过以下的例子就索引数组和关联数组进行一些讨论。
$arr=array(1,2,3,'name'=>'misko_lee','age'=>22); //定义一个索引、关联混合型数组
for($i=0;$i
Cout($arr)函数调用返回$arr数组的长度。但是for循环只能够正常输出索引数组。因而,我们可以判断,索引数组与关联数组是两种不同的实现。因而也证明了PHP中的数组是松散型的结构的猜想。
关于索引数组我们还有以下猜想:
$arr[100]=100;
$arr[]=101; //此时$arr[]自动增长的下标为101。
上面的实验证明了我们的猜想,PHP中的数组计数器并不是从数组内存首地址开始计数,而是一种伪计数方式。因此,利用计数器去判断数组大小是完成错误的。
以上言论是个人胡言乱语,具有十分不可靠的因素。欢迎各位学长童鞋指出错误。好人一生平安。
最后PS一句:数组使用foreach遍历后如果不是需求原因,请将数组指针利用reset()函数进行遍历重置

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 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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