Monday 3 September 2018

PHP: Is there a difference between Index, Element, Key, Value of a Array? & Hellip; 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.

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.

0 comments:

Post a Comment