PHP 中如何实现关联数组? [英] How are associative arrays implemented in PHP?

查看:32
本文介绍了PHP 中如何实现关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 PHP 是如何实现关联数组的?PHP 使用什么底层数据结构?PHP 是否对密钥进行散列并将其存储在某种散列映射中?我很好奇,因为我想知道在插入和搜索键时关联数组的性能如何.

Can someone explain how PHP implements associative arrays? What underlying data structure does PHP use? Does PHP hash the key and store it in some kind of hash map? I am curious because I was wondering what the performance of associative arrays where when inserting and searching for keys.

推荐答案

投票最高的答案链接已损坏,并没有给出太多解释.

The highest voted answer link is broken and doesn't give that much explanation.

PHP 是用 C 编写的,底层结构只是一个 C 数组.C 数组只是内存块.C 数组中的索引必须是连续的,不能有索引 0 和后面的索引 1000.为了使关联数组键起作用,在将它们添加到 C 数组之前,它们会通过哈希函数转换为正确的 C 索引.

PHP is written in C and the underlying structure is just a C array. C arrays are just chunks of memory. The indexes in C arrays must be continuous, you can't have an index 0 and an index 1000 that comes after it. To make associative array keys work, before they are added to the C array, they are converted to proper C indices via a hash function.

对于完整的解释,我发现这个链接提供了更多信息.

For a full explanation, I found this link to be much more informative.

http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

这篇关于PHP 中如何实现关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆