PHP中数组和哈希有什么区别?

发布于 2024-07-19 18:46:56 字数 224 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-07-26 18:46:56

您所描述的两者都是数组。 两者之间的唯一区别是您显式设置第二个键,因此它们被称为 关联数组。 我不知道您从哪里获得哈希术语(Perl?),但这不是它们在 PHP 中的名称。

因此,例如,如果您要执行此操作:

$foo = array(1,2,3,4,5);
print_r($foo);

输出将是:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

正如您所看到的,用于访问您输入的各个值的键是为您创建的,但仍然存在。 所以这个数组本质上也是关联的。 数组的其他“类型”的方式完全相同,只是您明确地说“我想使用 this 键访问 this 值”而不是自动数字索引(尽管您提供的密钥也可以是数字)。

$bar = array('uno' => 'one', 'dos' => 'two');
print_r($bar);

会输出:

Array
(
    [uno] => one
    [dos] => two
)

正如您所期望的,执行 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:

$foo = array(1,2,3,4,5);
print_r($foo);

The output would be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

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).

$bar = array('uno' => 'one', 'dos' => 'two');
print_r($bar);

Would output:

Array
(
    [uno] => one
    [dos] => two
)

As you might then expect, doing print $bar['one'] would output uno, and doing $foo[0] from the first example would output 1.

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.

相权↑美人 2024-07-26 18:46:56

实际上,php 中没有数组 - 只有关联数组(基本上是一个哈希表)

尝试

$ar=array("zero","one","two","three","four");
unset($ar[3]);

这样做将从数组中删除“三”,但您会注意到数组键(数组不是关联)将保持不变(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

$ar=array("zero","one","two","three","four");
unset($ar[3]);

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.

焚却相思 2024-07-26 18:46:56

进入php引擎的所有数组(关联的或顺序的)都是哈希表,这是因为它是读取单个元素最快的方法。 内部有创建和填充数组的基本函数:

int zend_hash_init(HashTable *ht, uint nSize,hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent);

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

......

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:

int zend_hash_init(HashTable *ht, uint nSize,hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent);

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

int zend_hash_add(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_update(HashTable *ht, char *arrKey, uinit nKeySize, void *pData, uinit nDataSize, void **pDest)

int zend_hash_index_update(HashTable *ht, ulong h, void *pDate, uinit nDataSize, void **pDest)

int zend_hash_next_index_insert(HashTable *ht, void *pData, uinit nDataSize, void **pDest)

......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文