Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

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.

.

share|improve this answer
 
thx.... though ur comment is hard to read [no formating] –  Universal Grasp Nov 23 '13 at 9:22
 
if you find it useful then please upvote it. –  Tarun Nov 23 '13 at 9:25
 
What do you mean by value.Key? –  Universal Grasp Nov 23 '13 at 9:27
 
So, should i want to check if $a or $b or $c exists or are put there what function should used is there any function in PHP to check if an Index exists or is available? –  Universal Grasp Nov 23 '13 at 9:31
1  
array_key_exists("0",array($a,$b,$c)),array_key_exists("1",array($a,$b,$c)),arra‌​y_key_exists("2",array($a,$b,$c)) or you can use isset –  Tarun Nov 23 '13 at 9:41
show 4 more comments

Key == Index, Element == Value

share|improve this answer
 
can u please Explain?... in an array like: array($a,$b,$c,$d=>$e) What will be What? –  Universal Grasp Nov 23 '13 at 9:05
add comment

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
)
share|improve this answer
 
What about the Element and the Key?... Why is there only index and value?... [I am really is confused]. –  Universal Grasp Nov 23 '13 at 9:16
 
As @enumag has already told you, "key" is analog to "index" and "element" is analog to "value". ;) –  Eduardo Casas Nov 23 '13 at 9:50
add comment

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

PHP docs:

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

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.