1

这是我用来抓取和处理 JSON 输入的函数:

<?php
$json = "http://pastebin.com/raw.php?i=ihAapq30";
$cache_lastfm = 'BLAHBLAHDIR/'.sha1($json).'.json';

if(file_exists($cache_lastfm) && filemtime($cache_lastfm) > time() - 1000){
    // if a cache file newer than 1000 seconds exists, use it
    $data = json_decode(file_get_contents($cache_lastfm), true);
} else {
    $data = json_decode(file_get_contents($json), true);
    file_put_contents($cache_lastfm,json_encode($data));
}

$data = $data['recenttracks'];
foreach ($data['track'] as $track) {
    $artist = $track['artist']['#text'];
    $title = $track['name'];
    $url = $track['url'];
echo '<li><a href="', $url, '" title="', $title, '">', $artist, ' - ', $title, '</li></a>'; }
?>

它完美地工作..我的问题是,我怎样才能只删除具有以下内容的“条目”:

        "@attr":{
           "nowplaying":"true"
        }

... “属性”?检查 pastebin 页面以了解我的意思:)

4

1 回答 1

2

请试试这个:

<?php
    $data = $data['recenttracks'];

    $tracks=$data['track'];

    foreach ($tracks as $index=>$track) {
        if (isset($track['@attr'])) {
            unset($tracks[$index]);
        }
    }

    foreach ($tracks as $track) {
        $artist = $track['artist']['#text'];
        $title  = $track['name'];
        $url    = $track['url'];
        echo '<li><a href="', $url, '" title="', $title, '">', $artist, ' - ', $title, '</li></a>';
    }
?>
于 2013-05-30T22:17:03.250 回答