"Ciaran" <cr*******@hotmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements and then call them by
the index?
var mycars = new Array(3)
mycars['bigone'] = "Saab"
mycars['biggerone'] = "Volvo"
mycars[2] = "BMW"
document.write(mycars[0]);
is printing nothing when in php the same idea would spit out "Saab"
That is because your first declare it as an "associative" array, which, whilst it does not
exist in JavaScript, is a way to access the values. I may be wording this incorrectly, so
I will give an example.
arr1['author'] = 'Stephen King';
arr1.push('0th');
Now, if you do:
alert(arr1[0]);
.... you get 0th. Only arr1['author'] would retrieve what you expect.
The best method of doing what you want (entirely associative) is to use JSON on an Object.
var parents = {};
parents = {
'daddy' : 'The Dad',
'mommy' : 'The Mom'
}
This actually led me to write a routine that took an indice and a value and wrote to the
array accordingly (thereby giving me what you know as an associative array).
-Lost