Php json解码数组

时间:2016-10-09 13:08:20

标签: php arrays json

我有一个json字符串。但我无法访问价值观。

$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$content = file_get_contents($json_string);
$json_a = json_decode($content, true);

echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];

我该如何修复此代码?谢谢

2 个答案:

答案 0 :(得分:1)

$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';

// you don't need this line
//$content = file_get_contents($json_string);
$json_a = json_decode($json_string, true);

echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];

答案 1 :(得分:1)

如果您将JSON存储在字符串中然后对其进行解码,则无需使用file_get_contents。遵循以下方法:

$json_string = '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$json_a = json_decode($json_string, true);

echo $json_a['05526']['0']['name']; // rapertuar
echo $json_a['05526']['0']['notes']['0']['mat1']; // 59