且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP中的数字数组和关联数组是两个不同的东西吗?

更新时间:2022-01-04 23:54:03

PHP中的所有数组都是相同的.它们被实现为将键与值相关联的哈希映射,无论键的类型是什么.

All arrays in PHP are the same; they're implemented as hash maps which associate keys to values, whatever type the keys may be.

手册:

索引和关联数组类型与PHP中的类型相同,都可以包含整数和字符串索引.

The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

但是,如果数组同时具有数字索引和非数字索引,我仍将其称为关联数组. 关联的"的含义在此不作赘述.仍然站立.

If an array had both numeric and non-numeric indices, though, I'd still call it an associative array. The meaning of "associative" still stands.

***:

关联数组是一种抽象数据类型,由一组唯一键和一组值组成,其中每个键与一个值(或一组值)相关联.

An associative array is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values).

...

从计算机程序员的角度来看,关联数组可以看作是数组的概括.常规数组将整数键(索引)映射到任意数据类型的值时,关联数组的键也可以任意键入.在某些编程语言(例如Python)中,关联数组的键甚至不必具有相同的类型.

From the perspective of a computer programmer, an associative array can be viewed as a generalization of an array. While a regular array maps an integer key (index) to a value of arbitrary data type, an associative array's keys can also be arbitrarily typed. In some programming languages, such as Python, the keys of an associative array do not even need to be of the same type.

对于最后一句话,同样适用于PHP,如您的示例所示.

For the last sentence, the same applies for PHP, as shown in your example.