PHP中“关联”和“索引”数组之间的实际差异是什么?

时间:2022-06-20 13:25:36

The PHP array type is actually more akin to an an ordered map than a traditional C array. It's PHP's original general use data structure. The manual goes as far to say The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

PHP数组类型实际上更类似于有序映射而不是传统C数组。它是PHP的原始通用数据结构。手册尽可能地说,索引和关联数组类型在PHP中是相同的类型,它们都可以包含整数和字符串索引。

However, there's a lot of cases where built-in language features will make a distinction between "indexed" arrays (arrays with sequential, integer keys) and "associative" arrays (arrays with non-sequential and/or keys of mixed types).

但是,在很多情况下,内置语言功能将区分“索引”数组(具有顺序,整数键的数组)和“关联”数组(具有非顺序和/或混合类型键的数组)。

One example of this is the array_merge function.

其中一个例子是array_merge函数。

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个键。但是,如果数组包含数字键,则后面的值不会覆盖原始值,但会附加。

If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.

如果只给出一个数组并且数组被数字索引,那么键将以连续的方式重新索引。

What are the other places in PHP where a distinction is made between indexed and associative arrays? I'm specifically interested in the Userland differences, although any insight into the Array implementation in PHP's source would be interesting as well.

PHP中的其他地方有哪些区别在索引和关联数组之间?我对Userland的差异特别感兴趣,尽管对PHP源代码中的Array实现有任何了解也很有趣。

5 个解决方案

#1


3  

The most prevalent one that comes to mind is that an indexed array can be looped over using a traditional for loop, whereas an associative one cannot (because it does not have the numeric indexes):

最常见的一个问题是索引数组可以使用传统的for循环进行循环,而关联数组则不能(因为它没有数字索引):

for ($i = 0; $i < count($indexed_array); $i++)
{
  // do something with $indexed_array[$i]
}

Of course, php also has a foreach keyword, which works the same on both types.

当然,php也有一个foreach关键字,它在两种类型上的工作方式相同。

#2


5  

Actually, any array, no matter if it's indexed or associative, is a hashtable (plus a doubly-linked list for maintaining the order of elements) in PHP. However, in userland PHP code, indexed and associative arrays almost always serve different purposes and sometimes need to be treated in different ways, so several functions like sort/asort make a distinction between them just for convenience.

实际上,任何数组,无论是索引还是关联,都是PHP中的哈希表(加上用于维护元素顺序的双向链表)。但是,在userland PHP代码中,索引和关联数组几乎总是用于不同的目的,有时需要以不同的方式处理,因此sort / asort等几个函数仅为方便起见区分它们。

#3


3  

.. and then there is SplFixedArray, starting from 5.3, it supports only integer indexes, has fixed size and is generally faster than the native arrays.

..然后有SplFixedArray,从5.3开始,它只支持整数索引,具有固定的大小,并且通常比本机数组更快。

#4


2  

One interesting difference I've found is when using json_encode.

我发现一个有趣的区别是使用json_encode。

json_encode(array(0=>0,1=>1,2=>2));
> [0,1,2]
json_encode(array(0=>0,2=>2));
> {"0":0,"2":2}

As a lone example this makes sense, but it's more surprising when combined with, say, array_filter.

作为一个单独的例子,这是有道理的,但是当与array_filter结合使用时更令人惊讶。

$f = function($x) { return $x != 1; };
json_encode(array_filter(array(0,1,2), $f));
> {"0":0,"2":2}

We started off with a numeric array, filtered some elements out, but the resulting json is an associative array!

我们从数字数组开始,过滤掉一些元素,但生成的json是一个关联数组!


Note that we can get the desired json by using array_values.

请注意,我们可以使用array_values获取所需的json。

json_encode(array_values(array_filter(array(0,1,2),$f)));
> [0,2]

#5


0  

Pretty much all of the core sorting functions (with all the sort, ksort, asort variations depending on whether you want to maintain key association and so on).

几乎所有核心​​排序功能(所有排序,ksort,asort变化取决于您是否要维护密钥关联等)。

#1


3  

The most prevalent one that comes to mind is that an indexed array can be looped over using a traditional for loop, whereas an associative one cannot (because it does not have the numeric indexes):

最常见的一个问题是索引数组可以使用传统的for循环进行循环,而关联数组则不能(因为它没有数字索引):

for ($i = 0; $i < count($indexed_array); $i++)
{
  // do something with $indexed_array[$i]
}

Of course, php also has a foreach keyword, which works the same on both types.

当然,php也有一个foreach关键字,它在两种类型上的工作方式相同。

#2


5  

Actually, any array, no matter if it's indexed or associative, is a hashtable (plus a doubly-linked list for maintaining the order of elements) in PHP. However, in userland PHP code, indexed and associative arrays almost always serve different purposes and sometimes need to be treated in different ways, so several functions like sort/asort make a distinction between them just for convenience.

实际上,任何数组,无论是索引还是关联,都是PHP中的哈希表(加上用于维护元素顺序的双向链表)。但是,在userland PHP代码中,索引和关联数组几乎总是用于不同的目的,有时需要以不同的方式处理,因此sort / asort等几个函数仅为方便起见区分它们。

#3


3  

.. and then there is SplFixedArray, starting from 5.3, it supports only integer indexes, has fixed size and is generally faster than the native arrays.

..然后有SplFixedArray,从5.3开始,它只支持整数索引,具有固定的大小,并且通常比本机数组更快。

#4


2  

One interesting difference I've found is when using json_encode.

我发现一个有趣的区别是使用json_encode。

json_encode(array(0=>0,1=>1,2=>2));
> [0,1,2]
json_encode(array(0=>0,2=>2));
> {"0":0,"2":2}

As a lone example this makes sense, but it's more surprising when combined with, say, array_filter.

作为一个单独的例子,这是有道理的,但是当与array_filter结合使用时更令人惊讶。

$f = function($x) { return $x != 1; };
json_encode(array_filter(array(0,1,2), $f));
> {"0":0,"2":2}

We started off with a numeric array, filtered some elements out, but the resulting json is an associative array!

我们从数字数组开始,过滤掉一些元素,但生成的json是一个关联数组!


Note that we can get the desired json by using array_values.

请注意,我们可以使用array_values获取所需的json。

json_encode(array_values(array_filter(array(0,1,2),$f)));
> [0,2]

#5


0  

Pretty much all of the core sorting functions (with all the sort, ksort, asort variations depending on whether you want to maintain key association and so on).

几乎所有核心​​排序功能(所有排序,ksort,asort变化取决于您是否要维护密钥关联等)。