• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

深入理解PHP之数组遍历顺序

php 搞代码 3年前 (2022-01-23) 26次浏览 已收录 0个评论

经常会有人问我, PHP的数组, 如果用foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢?
比如:

  1. <?php
  2. $arr['laruence'] = 'huixinchen';
  3. $arr['yahoo'] = 2007;
  4. $arr['baidu'] = 2008;
  5. foreach ($arr as $key => $val) {
  6. //结果是什么?
  7. }

又比如:

  1. <?php
  2. $arr[2] = 'huixinchen';
  3. $arr[1] = 2007;
  4. $arr[0] = 2008;
  5. foreach ($arr as $key => $val) {
  6. //现在结果又是什么?
  7. }

要完全了解清楚这个问题, 我想首先应该要大家了解PHP数组的内部实现结构………

PHP的数组

在PHP中, 数组是用一种HASH结构(HashTable)来实现的, PHP使用了一些机制, 使得可以在O(1)的时间复杂度下实现数组的增删, 并同时支持线性遍历和随机访问.

之前的文章中也讨论过, PHP的HASH算法, 基于此, 我们做进一步的延伸.

认识HashTable之前, 首先让我们看看HashTable的结构定义, 我加了注释方便大家理解:

  1. typedef struct _hashtable {
  2. uint nTableSize; /* 散列表大小, Hash值的区间 */
  3. uint nTableMask; /* 等于nTableSize -1, 用于快速定位,相当于是取模 */
  4. uint nNumOfElements; /* HashTable中实际元素的个数 */
  5. ulong nNextFreeElement; /* 下个空闲可用位置的数字索引,执行$arr[]='xxx'时使用这个值 */
  6. Bucket *pInternalPointer; /* 内部位置指针, 会被reset, current这些遍历函数使用 */
  7. Bucket *pListHead; /* 头元素, 用于线性遍历 */
  8. Bucket *pListTail; /* 尾元素, 用于线性遍历 */
  9. Bucket **arBuckets; /* 实际的存储容器Bucket **arBuckets定义一个指针数组的另一种方式) */
  10. dtor_func_t pDestructor;/* 元素的析构函数(指针) */
  11. zend_bool persistent;
  12. unsigned char nApplyCount; /* 循环遍历保护 */
  13. zend_bool bApplyProtection;
  14. #if ZEND_DEBUG
  15. int inconsistent;
  16. #endif
  17. } HashTable;

关于nApplyCount的意义, 我们可以通过一个例子来了解:

  1. <?php
  2.     $arr = array(1,2,3,4,5,);
  3.     $arr[] = &$arr;
  4.  
  5.     var_export($arr); //Fatal error: Nesting level too deep - recursive dependency?

这个字段就是为了防治循环引用导致的无限循环而设立的.

查看上面的结构, 可以看出, 对于HashTable, 关键元素就是arBuckets了, 这个是实际存储的容器, 让我们来看看它的结构定义:

  1. typedef struct bucket {
  2. ulong h; /* 数字索引/hash值 */
  3. uint nKeyLength; /* 字符索引的长度 */
  4. void *pData; /* 数据,指向zval结构 */
  5. void *pDataPtr; /* 数据指针 */
  6. struct bucket *pListNext; /* 下一个元素, 用于线性遍历 */
  7. struct bucket *pListLast; /* 上一个元素, 用于线性遍历 */
  8. struct bucket *pNext; /* 处于同一个拉链中的下一个元素 */
  9. struct bucket *pLast; 本文来源gaodai#ma#com搞@@代~&码*网/搞gaodaima代码 /* 处于同一拉链中的上一个元素 */
  10. char arKey[1]; /* 节省内存,方便初始化的技巧 */
  11. } Bucket;

我们注意到, 最后一个元素, 这个是flexible array技巧, 可以节省内存,和方便初始化的一种做法, 有兴趣的朋友可以google flexible array.

There are a couple of things it could refer to. The only place I’ve seen that precise wording used much is C99 though.

A flexible array member is the official C99 name for what used to (usually) be called the “struct hack”. The basic idea is that you define a struct something like this:

struct x { int a; // whatever members you want here. size_t size; int x[]; // no size, last member only};This is used primarily (or exclusively) with dynamic allocation. When you want to allocate an object of this type, you


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:深入理解PHP之数组遍历顺序

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址