PHP:数组的索引,元素,键,值之间有区别吗?......它们是一样的吗?

时间:2022-07-27 11:45:10

When dealing with PHP arrays, I quite often here terms such as:

在处理PHP数组时,我经常在这里使用以下术语:

Array Key,

Array Index,

Array Element,

Array Value

Can someone, PLEASE , in simple terms explain what each of these basically means?

有人,请简单地解释一下这些基本上意味着什么?

Is there any difference?... are they all referring to the same thing?

有什么不同吗?......他们都是指同一件事吗?

Where do you use which? and when?

你在哪里用哪个?什么时候?

Any clarification with some simple use case examples will be highly appreciated.

任何有关一些简单用例的澄清都将受到高度赞赏。

i.e: in an array like: array($a,$b,$c,$d=>$e) What will be What?

即:在数组中:数组($ a,$ b,$ c,$ d => $ e)什么是什么?

Thanks in advance.

提前致谢。

4 个解决方案

#1


3  

An array is a collection of Elements.
Every element has key & value. Key can be a integer(index) or a string.
In you case

数组是元素的集合。每个元素都有关键和价值。键可以是整数(索引)或字符串。在你的情况下

array($a, $b, $c, $d=>$e)

数组($ a,$ b,$ c,$ d => $ e)

can be rewritten as

可以改写为

array(0 => $a, 1 => $b, 2 => $c, $d => $e);  

Where 0, 1, 2, $d are the keys of the array.
You can refer 0, 1, 2 as a index for value $a,$b,$c respectively and $d is a key for $e.

其中0,1,2,$ d是数组的键。您可以将0,1,2分别作为$ a,$ b,$ c的索引,$ d是$ e的密钥。

.

#2


0  

Key == Index, Element == Value

键==索引,元素==值

#3


0  

That would be:

那将是:

array(
    0  => $a, // index: 0, value : $a
    1  => $b, // index: 1, value : $b
    2  => $c, // index: 2, value : $c
    $d => $e  // index: $d, value : $e
)

#4


0  

In my experience most PHP documentation uses the key => value configuration, while index: element is more common in JavaScript and jQuery.

根据我的经验,大多数PHP文档使用key => value配置,而index:element在JavaScript和jQuery中更常见。

PHP docs:

http://us2.php.net/manual/en/language.types.array.php

JavaScript Docs (Mozilla):

JavaScript Docs(Mozilla):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

They both apply to the same concept where objects in an array have an index or key, and subsidiary objects, elements or values attached to that key.

它们都适用于相同的概念,其中数组中的对象具有索引或键,以及附加到该键的辅助对象,元素或值。

#1


3  

An array is a collection of Elements.
Every element has key & value. Key can be a integer(index) or a string.
In you case

数组是元素的集合。每个元素都有关键和价值。键可以是整数(索引)或字符串。在你的情况下

array($a, $b, $c, $d=>$e)

数组($ a,$ b,$ c,$ d => $ e)

can be rewritten as

可以改写为

array(0 => $a, 1 => $b, 2 => $c, $d => $e);  

Where 0, 1, 2, $d are the keys of the array.
You can refer 0, 1, 2 as a index for value $a,$b,$c respectively and $d is a key for $e.

其中0,1,2,$ d是数组的键。您可以将0,1,2分别作为$ a,$ b,$ c的索引,$ d是$ e的密钥。

.

#2


0  

Key == Index, Element == Value

键==索引,元素==值

#3


0  

That would be:

那将是:

array(
    0  => $a, // index: 0, value : $a
    1  => $b, // index: 1, value : $b
    2  => $c, // index: 2, value : $c
    $d => $e  // index: $d, value : $e
)

#4


0  

In my experience most PHP documentation uses the key => value configuration, while index: element is more common in JavaScript and jQuery.

根据我的经验,大多数PHP文档使用key => value配置,而index:element在JavaScript和jQuery中更常见。

PHP docs:

http://us2.php.net/manual/en/language.types.array.php

JavaScript Docs (Mozilla):

JavaScript Docs(Mozilla):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

They both apply to the same concept where objects in an array have an index or key, and subsidiary objects, elements or values attached to that key.

它们都适用于相同的概念,其中数组中的对象具有索引或键,以及附加到该键的辅助对象,元素或值。