多维数组json_decode(第一级)

时间:2013-08-08 17:06:09

标签: php arrays json associative

我有一个像这样的JSON文件:

{
    "numeric1": {
        "id": "numeric1",
        "name": "alphanumeric1",
        "key": "alphanumeric2",
        "expire": "alphanumeric3",
        "status": true,
        "ads": true
    },

    etc...
}

with(etc ...)我的意思是矩阵重复多次。 我用PHP解析它:

$allowed = json_decode(file_get_contents("allowed.json"), true);

然后我得到一个像:

这样的数组
Array
(
    [0] => Array
        (
            [id] => numeric1
            [name] => alphanumeric1
            [key] => alphanumeric2
            [expire] => alphanumeric3
            [status] => 1
            [ads] => 1
        )

     etc....
 )

所以我失去了第一级关联键,我有

 [0] => Array
而不是
 ["numeric1"] => Array

如何保留我的JSON数组的第一级?感谢。

1 个答案:

答案 0 :(得分:2)

试试这个:

$allowed = (array) json_decode(file_get_contents("allowed.json"));

因此,不是直接将JSON解析为数组(通过指定json_decode的第二个参数),而是首先获取将保留键的对象,然后将其转换为数组。