如何将这段可怕的代码转换为关联数组? :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>
答案 0 :(得分:14)
second argument to json_decode
强制JSON在关联数组中反序列化(如果它是真实的(默认情况下是假的)。
即,使用json_decode($json, true)