简体   繁体   English

将PHP关联数组转换为JSON关联数组

[英]Converting a PHP associative array to a JSON associative array

I am converting a look-up table in PHP which looks like this to JavaScript using json_encode: 我正在使用json_encode将PHP中的查找表转换为JavaScript:

 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: 在JSON中,它是:

{"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. 问题是我得到一个JS对象,而不是一个数组,标识符是一个数字。 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. 我看到了解决这个问题的两种方法 - 要么找到一种方法来使用数字访问JSON(我不知道怎么做),要么让json_encode(或其他一些自定义编码函数)可以提供JavaScript关联数组。

(Yes, I am rather lacking in my JavaScript department). (是的,我的JavaScript部门很缺乏)。

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. 注意 :JSON输出与数组不匹配 - 这是因为我在下标中对每个元素执行手动json编码,然后将其推送到数组(以索引作为键),然后在其上使用json_encode。 To be clear, the number are not sequential because it's an associative array (which is why the JSON output is not an array). 要清楚,数字不是顺序的,因为它是一个关联数组(这就是JSON输出不是数组的原因)。

array('a', 'b', 'c') encodes as ['a', 'b', 'c'], 数组('a','b','c')编码为['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 也许转换为object而不是array的原因是你将php数组从1索引而不是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. JavaScript对象属性名称可以是任何格式的字符串。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM