最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting a PHP associative array to a JSON associative array - Stack Overflow

programmeradmin4浏览0评论

I am converting a look-up table in PHP which looks like this to JavaScript using json_encode:

 AbilitiesLookup Object
(
[abilities:private] => Array
    (
        [1] => Ability_MeleeAttack Object
            (
                [abilityid:protected] => 
                [range:protected] => 1
                [name:protected] => MeleeAttack
                [ability_identifier:protected] => MeleeAttack
                [aoe_row:protected] => 1
                [aoe_col:protected] => 1
                [aoe_shape:protected] => 
                [cooldown:protected] => 0
                [focusCost:protected] => 0
                [possibleFactions:protected] => 2
                [abilityDesc:protected] => Basic Attack
            )
            .....snipped...

And in JSON, it is:

{"1":{"name":"MeleeAttack","fof":"2","range":"1","aoe":[null,"1","1"],"fp":"0","image":"dummy.jpg"},....

The problem is I get a JS object, not an array, and the identifier is a number. I see 2 ways around this problem - either find a way to access the JSON using a number (which I do not know how) or make it such that json_encode (or some other custom encoding functions) can give a JavaScript associative array.

(Yes, I am rather lacking in my JavaScript department).

Note: The JSON output doesn't match the array - this is because I do a manual json encoding for each element in the subscript, before pushing it onto an array (with the index as the key), then using json_encode on it. To be clear, the number are not sequential because it's an associative array (which is why the JSON output is not an array).

I am converting a look-up table in PHP which looks like this to JavaScript using json_encode:

 AbilitiesLookup Object
(
[abilities:private] => Array
    (
        [1] => Ability_MeleeAttack Object
            (
                [abilityid:protected] => 
                [range:protected] => 1
                [name:protected] => MeleeAttack
                [ability_identifier:protected] => MeleeAttack
                [aoe_row:protected] => 1
                [aoe_col:protected] => 1
                [aoe_shape:protected] => 
                [cooldown:protected] => 0
                [focusCost:protected] => 0
                [possibleFactions:protected] => 2
                [abilityDesc:protected] => Basic Attack
            )
            .....snipped...

And in JSON, it is:

{"1":{"name":"MeleeAttack","fof":"2","range":"1","aoe":[null,"1","1"],"fp":"0","image":"dummy.jpg"},....

The problem is I get a JS object, not an array, and the identifier is a number. I see 2 ways around this problem - either find a way to access the JSON using a number (which I do not know how) or make it such that json_encode (or some other custom encoding functions) can give a JavaScript associative array.

(Yes, I am rather lacking in my JavaScript department).

Note: The JSON output doesn't match the array - this is because I do a manual json encoding for each element in the subscript, before pushing it onto an array (with the index as the key), then using json_encode on it. To be clear, the number are not sequential because it's an associative array (which is why the JSON output is not an array).

Share Improve this question asked Mar 9, 2010 at 6:38 ExtrakunExtrakun 19.3k23 gold badges84 silver badges137 bronze badges 1
  • 1 Arrays in JavaScript always have idexes squential and starting from zero. Javascript object is an associative array. There is no other type (other then object) in javascript that behaves like associative array. – Kamil Szot Commented Mar 9, 2010 at 6:51
Add a comment  | 

2 Answers 2

Reset to default 12

array('a', 'b', 'c') encodes as ['a', 'b', 'c'],

Maybe the reason of conversion to object instead of array is that you index your php array from 1 not from 0

I've checked and

<?php
echo json_encode(array('a', 'b', 'c'))."\n";
echo json_encode(array(0 => 'a', 'b', 'c'))."\n"; // same as above but explicit
echo json_encode(array(1 => 'a', 'b', 'c'))."\n";

gives

["a","b","c"]
["a","b","c"]
{"1":"a","2":"b","3":"c"}

JavaScript object property names can be strings in any format. They can even be just digits (as it is in your case) and there's no issue with accessing them with numeric indexes/keys:

var obj = {
    "1": "foo",
    "2": "bar"
};

obj["1"]; // returns "foo"
obj[1]; // returns "foo" (1 will implicitly get cast to the string "1")
发布评论

评论列表(0)

  1. 暂无评论