简体   繁体   English

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

[英]PHP: is there difference between Index, Element, Key, Value of an Array?… are they the same thing?

When dealing with PHP array s, I quite often here terms such as: 在处理PHP array ,我经常在这里使用以下术语:

Array Key , Array Key

Array Index , Array Index

Array Element , Array Element

Array Value 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. 任何有关一些简单用例的澄清都将受到高度赞赏。

ie: in an array like: array($a,$b,$c,$d=>$e) What will be What? 即:在数组中: array($a,$b,$c,$d=>$e)什么是什么?

Thanks in advance. 提前致谢。

An array is a collection of Elements. 数组是Elements的集合。
Every element has key & value. 每个元素都有关键和价值。 Key can be a integer(index) or a string. 键可以是整数(索引)或字符串。
In you case 在你的情况下

array($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. 其中0,1,2,$ d是数组的键。
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分别称为$ a,$ b,$ c的索引,$ d是$ e的密钥。

.

Key == Index,Element == Value

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
)

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: PHP文档:

http://us2.php.net/manual/en/language.types.array.php 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 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. 它们都适用于相同的概念,其中数组中的对象具有索引或键,以及附加到该键的辅助对象,元素或值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM