1

How to create such a PHP array in JavaScript?

$arr = array('oneKey' => array('key1' => 'value1',
                               'key2' => 'value2'),
             'anotherKey' => array('key1' => 'value1',
                                  'key2' => 'value2'));

EDIT: Guys, I forgot to mention that I would then need a simple way to sort those array('key1' => 'value1', 'key2' => 'value2') lexicographically by its keys.

EDIT2: Actually I won't "convert" it. It's a way I explain things. I am more of a php guy.

5
  • Edited for syntax errors. Hopefully what is here now is what you intended. Commented May 31, 2010 at 13:43
  • 1
    Your question doesn't make sense to me anymore. Your goal is to sort, not convert to Javascript? Then why did you ask about converting to Javascript? Commented May 31, 2010 at 13:59
  • I improved the question. You don't want to convert it. You want to create it (roughly) the same way. Commented May 31, 2010 at 14:01
  • Matchu, because before sorting it I need to construct it. Commented May 31, 2010 at 14:12
  • But if you're going to sort the keys anyway, you don't have any inherent ordering, so you don't need an ordered mapping. What are you actually trying to do? Commented May 31, 2010 at 14:32

3 Answers 3

5

If you're sending it over AJAX, consider encoding it in JSON and parsing it back into an array on the javascript side. For the record, it's more of an object in Javascript, since it has keys and values.

In PHP:

$jsonString = json_encode($arr);

Then in JS:

var jsonObject = JSON.parse(str);

Many JS libraries have JSON parsers available to them. Otherwise, grab the one at the above link. No eval().


On sorting, simply specify the keys in the order you want them in PHP, and they should come back intact.

Sign up to request clarification or add additional context in comments.

4 Comments

Better answer. +1, answer revoked.
I am not going to send it anywhere.
Hmm... Actually I won't "convert" it. It's a way I explain things. I am more of a php guy...
"simply specify the keys in the order you want them in PHP" - sorry, no PHP here. It is just my way of explaining things.
2
<?php


$arr = array('oneKey' => array('key1' => 'value1',
                               'key2' => 'value2'),
             'anotherKey' => array('key1' => 'value1',
                                  'key2' => 'value2'));
?>

<script type="text/javascript">
/* <![CDATA[ */

var _my_var = '<?= json_encode($arr) ?>';

/* ]]> */
</script>

EDIT:

if you need to have the keys ordered I recomend you use ksort on the php side before use it with javascript

2 Comments

I was going to edit to fix the formatting, but there's nothing there, from what I can see...
I submited the answer by mistake, now can you please remove the down vote?
2

Create a JS object. The {} signifies start and end of an object and the : signifies a key-value separator, the , signifies a property (key-value pair) separator.

var obj = {
    'oneKey': {
        'key1': 'value1',
        'key2': 'value2'
    },
    'anotherKey': {
        'key1': 'value1',
        'key2': 'value2'
    }
};

alert(obj.oneKey.key2); // value2
alert(obj['anotherKey']['key1']); // value1

See also:

3 Comments

That way I can't sort, say, the anotherKey object lexicographically by its keys, right? I would need something like this: obj['anotherKey'].sort();
I have done the way you suggested and got stuck with sorting.
Then use a real array with index. JS doesn't support associative arrays like PHP does. See also hunlock.com/blogs/Mastering_Javascript_Arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.