我正在尝试转换此字符串:
$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字符串转换为对象数组?
答案 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);