PHP中数组和哈希有什么区别?
PHP 数组和哈希有什么区别?
数组: array(1,2,3...)
一个哈希: array(key1=value1, key2=value2, ...)
它们是不同还是相同?
※ 例如,函数参数allows array对hash是否有效?
因为我用常规语言来区分并使用它,所以我感到困惑。
What are the differences of an Array and a Hash PHP?
An array: array(1,2,3...)
A hash: array(key1=value1, key2=value2, ...)
are they different or the same?
※ For example, will the function arguments allows array be effective for the hash?
Because I distinguish it by the conventional language and used it, I am puzzled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所描述的两者都是数组。 两者之间的唯一区别是您显式设置第二个键,因此它们被称为 关联数组。 我不知道您从哪里获得哈希术语(Perl?),但这不是它们在 PHP 中的名称。
因此,例如,如果您要执行此操作:
输出将是:
正如您所看到的,用于访问您输入的各个值的键是为您创建的,但仍然存在。 所以这个数组本质上也是关联的。 数组的其他“类型”的方式完全相同,只是您明确地说“我想使用 this 键访问 this 值”而不是自动数字索引(尽管您提供的密钥也可以是数字)。
会输出:
正如您所期望的,执行
print $bar['one']
将输出uno
,执行$foo[0]
第一个示例将输出1
。就函数而言,PHP 函数大多数时候会采用这些“类型”的数组之一并执行您希望它们执行的操作,但需要注意一些区别,因为某些函数会对索引执行一些奇怪的操作有些人不会。 通常最好在使用数组函数之前阅读文档,因为它会说明输出将取决于数组的键。
您应该阅读手册以获取更多信息。
Both the things you are describing are arrays. The only difference between the two is that you are explicitly setting the keys for the second one, and as such they are known as associative arrays. I do not know where you got the Hash terminology from (Perl?) but that's not what they are referred as in PHP.
So, for example, if you were to do this:
The output would be:
As you can see, the keys to access the individual values you put in were created for you, but are there nonetheless. So this array is, essentially, associative as well. The other "type" of array is exactly the same way, except you are explcitily saying "I want to access this value with this key" instead of automatic numeric indexes (although the key you provide could also be numeric).
Would output:
As you might then expect, doing
print $bar['one']
would outputuno
, and doing$foo[0]
from the first example would output1
.As far as functions go, PHP functions will most of the time take either one of these "types" of array and do what you want them to, but there are distinctions to be aware of, as some functions will do funky stuff to your indexes and some won't. It is usually best to read the documentation before using an array function, as it will note what the output will be depending on the keys of the array.
You should read the manual for more information.
实际上,php 中没有数组 - 只有关联数组(基本上是一个哈希表)
尝试
这样做将从数组中删除“三”,但您会注意到数组键(数组不是关联)将保持不变(0,1,2,4) - 在任何正常语言中,它会将“4”的键重新编号为 3。
In actuality, there are no arrays in php - there are only associative arrays (which is basically a hash table)
Try doing
doing so will remove "three" from the array, but you'll notice that the array keys (the array is not associative) will remain the same (0,1,2,4) - in any normal language it would renumber the key for "four" to 3.
进入php引擎的所有数组(关联的或顺序的)都是哈希表,这是因为它是读取单个元素最快的方法。 内部有创建和填充数组的基本函数:
......
Into the engine php all arrays (associative or sequential ) are hash tables, and this because it is the fastest method in the reading of the single element. Internally there are basic functions to create and popolate an array:
......