-1

如何将这段可怕的代码转换为关联数组?:D

<?php
$json = file_get_contents('http://pastebin.com/NLHGMapT');
$track_data = json_decode($json);
?>
<ul>
<?php
    foreach ($track_data as $data)

            $text0 = $data->track[0]->artist->{'#text'};
            $text1 = $data->track[1]->artist->{'#text'};
            $text2 = $data->track[2]->artist->{'#text'};
            $text3 = $data->track[3]->artist->{'#text'};
            $artist0 = $data->track[0]->name;
            $artist1 = $data->track[1]->name;
            $artist2 = $data->track[2]->name;
            $artist3 = $data->track[3]->name;
            $url0 = $data->track[0]->url;
            $url1 = $data->track[1]->url;
            $url2 = $data->track[2]->url;
            $url3 = $data->track[3]->url;
        echo '<li><a href="'; echo $url0; echo '"><span>'; echo $text0; echo '</span> - <span>'; echo $artist0; echo '</span></li></a>';
        echo '<li><a href="'; echo $url1; echo '"><span>'; echo $text1; echo '</span> - <span>'; echo $artist1; echo '</span></li></a>';
        echo '<li><a href="'; echo $url2; echo '"><span>'; echo $text2; echo '</span> - <span>'; echo $artist2; echo '</span></li></a>';
        echo '<li><a href="'; echo $url3; echo '"><span>'; echo $text3; echo '</span> - <span>'; echo $artist3; echo '</span></li></a>';
?>
</ul>
4

1 回答 1

15

如果JSON 为真(默认为假),则强制 JSON 反序列化为关联数组的第二个参数。json_decode

也就是说,使用json_decode($json, true)

于 2013-05-28T15:40:27.983 回答