By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

PHP-Like associative array initialization ?

P: n/a
Is is possible to initialize a javascript associative array inline, as
you would do in PHP, e.g. :

<?php
$a = array("abc" => "def", "ghi" => "jkl");
?>
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Remi Bastide wrote:
Is is possible to initialize a javascript associative array inline, as
you would do in PHP, e.g. :

<?php
$a = array("abc" => "def", "ghi" => "jkl");
?>


Oops... forget about "associative arrays", as they sometimes cause
flaming discussion down here. Just call them objects (javascript
objects), and think of them as (imperfect) associative arrays.

As for the initialisation syntax, which is the javascript object
initialisation syntax ( see http://www.json.org ), you should be fine with :

a = { "abc": "def",
"ghi": "jkl" };

The quotes are optionnal, so the following is fine, too:

a= { abc: "def",
ghi: "jkl" }

However, if one of your keys has spaces or weird characters in it, se
the quotes:

cplx = { "abc d": "efg",
hij: "klm" };

Hope this helps,
Alexis
Jul 23 '05 #2

P: n/a
Just what I needed, thanks.
Alexis Nikichine <al**************@somedomain.fr> wrote:
Remi Bastide wrote:
Is is possible to initialize a javascript associative array inline, as
you would do in PHP, e.g. :

<?php
$a = array("abc" => "def", "ghi" => "jkl");
?>


Oops... forget about "associative arrays", as they sometimes cause
flaming discussion down here. Just call them objects (javascript
objects), and think of them as (imperfect) associative arrays.

As for the initialisation syntax, which is the javascript object
initialisation syntax ( see http://www.json.org ), you should be fine with :

a = { "abc": "def",
"ghi": "jkl" };

The quotes are optionnal, so the following is fine, too:

a= { abc: "def",
ghi: "jkl" }

However, if one of your keys has spaces or weird characters in it, se
the quotes:

cplx = { "abc d": "efg",
hij: "klm" };

Hope this helps,
Alexis


Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.