Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

Thanks in advance.

like image 507
Universal Grasp Avatar asked Nov 23 '13 08:11

Universal Grasp


1 Answers

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)

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.

.

like image 184
Tarun Avatar answered Oct 13 '22 02:10

Tarun