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

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

本文介绍了PHP中的数字数组和关联数组是两个不同的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里遇到的先前问题的更深层探讨:

This is a deeper dive into a previous question I had here: Can items in PHP associative arrays not be accessed numerically (i.e. by index)?

根据W3Schools ,:

According to W3Schools, :

  • 数字数组-具有数字索引的数组
  • 关联数组-每个ID键与一个值关联的数组
  • 多维数组-包含一个或多个数组的数组
  • 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

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

But is this accurate? Each element in the array can be assigned either an index or a string as a key- so what happens when the two are mixed in the same array?

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

推荐答案

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.

手册:

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

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.

维基百科:

...

从计算机程序员的角度来看,关联数组可以看作是数组的概括.常规数组将整数键(索引)映射到任意数据类型的值时,关联数组的键也可以任意键入.在某些编程语言(例如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.

这篇关于PHP中的数字数组和关联数组是两个不同的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:23