需要帮助解析使用PHP解析JSON解码的孙子孙女

时间:2014-07-03 11:08:49

标签: php json parsing grandchild

foreach($response_2['items'] as $item) {
$item['track']['name'] = (array)$item['track']['name'];
foreach($item['track']['name'] as $key => $value) {
    echo $value . "<br />";
}
$item['track']['artists']['name'] = (array)$item['track']['artists']['name'];
foreach($item['track']['artists']['name'] as $key => $value) {
    echo $value . "<br />";
} 
$item['track']['duration_ms'] = (array)$item['track']['duration_ms'];
foreach($item['track']['duration_ms'] as $key => $value) {
    echo $value . "<br />";
    } 
}

...解析此回复:http://pastebin.com/uxBF3Kxe#(在我解码之后)。

['track']['name']['track']['duration_ms']的所有值都会正确回显,但['track']['artists']['name']没有回显任何内容。

我在这里做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

['track']['artists']是数组:)

试试这个;)

foreach($item['track']['artists'] as $key => $value) {
    echo $value['name'] . "<br />";
}

...顺便说一句 这是更好的解决方案;)

$response_2 = json_decode($json, true);

foreach($response_2['items'] as $item) {
    echo $item['track']['name'] . "<br />";

    foreach($item['track']['artists'] as $key => $value) {
        echo $value['name'] . "<br />";
    }

    echo $item['track']['duration_ms'] . "<br />";
}

/ **添加了分隔符 * /

$response_2 = json_decode($json, true);

foreach($response_2['items'] as $item) {
    echo $item['track']['name'] . "<br />";

    echo implode(',', array_map(
            function ($value) {
                return $value['name'];
            },
            $item['track']['artists']
        )
    );

    echo "<br />";

    echo $item['track']['duration_ms'] . "<br />";
}