gpt4 book ai didi

php - PHP 中的数值数组和关联数组是两种不同的东西吗?

转载 作者:可可西里 更新时间:2023-10-31 23:54:25 27 4
gpt4 key购买 nike

这是对我之前在这里遇到的问题的更深入探讨:Can items in PHP associative arrays not be accessed numerically (i.e. by index)?

根据 W3School,:

In PHP, there are three kind of arrays:

  • Numeric array - An array with a numeric index
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

但这准确吗?数组中的每个元素都可以分配一个索引或一个字符串作为键 - 那么当两者混合在同一个数组中时会发生什么?

$myArray[0] = 'value1';
$myArray['one'] = 'value2';

最佳答案

PHP 中的所有数组都是相同的;它们被实现为将键与值相关联的 HashMap ,无论键是什么类型。

Manual :

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

但是,如果一个数组同时具有数字和非数字索引,我仍将其称为关联数组。 “联想”的意义依然存在。

Wikipedia :

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

...

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,如您的示例所示。

关于php - PHP 中的数值数组和关联数组是两种不同的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5931206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com