对象数组作为String到对象数组(jSON)?

时间:2012-03-29 17:36:29

标签: php

我正在尝试转换此字符串:

$json = '[{"a":1,"b":2,"c":3,"d":4,"e":5}, {"a":6,"b":7,"c":8,"d":9,"e":10}]';

到一个对象数组。我试过了:

$test = json_decode($json, true);
echo sizeof($test); //traces 2 !
echo $test[0]["a"]; //doesn't echo anything!

如何在PHP中将json字符串转换为对象数组?

2 个答案:

答案 0 :(得分:2)

假设将json解析为对象数组,请尝试

$test[0]->a

您可以使用

轻松查看
print_r($test)

将输出

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
            [e] => 5
        )

    [1] => Array
        (
            [a] => 6
            [b] => 7
            [c] => 8
            [d] => 9
            [e] => 10
        )

)

答案 1 :(得分:1)

json_decode返回object。要将对象转换为数组:

$test = (array)json_decode($json, true);