Reputation: 7059
Here's the function I'm using to grab and process a JSON input:
<?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>'; }
?>
It works perfectly.. my question is, how can I remove only the "entry" that has the:
"@attr":{
"nowplaying":"true"
}
... "attribute"? Check the pastebin page to understand what I mean :)
Upvotes: 1
Views: 1181
Reputation: 10975
Please try this:
<?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>';
}
?>
Upvotes: 2