1

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 :)

2
  • Why not use the plain old unset($data['recenttracks']['@attr']);, before entering the loop? Commented May 30, 2013 at 22:45
  • I added it just before foreach and after $data = $data['recenttracks']; but the result is the same (entry with "nowplaying" is still present).. Commented May 30, 2013 at 22:53

1 Answer 1

2

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>';
    }
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply! I need to remove the entry only if it has "nowplaying" attribute (which is "under" @attr). How can I modify your reply with this specification? :)
Check the update, The code works fine on my part, I just changed the formatting for testing.
Oh, it's my fault.. I explained myself badly. I need the whole.. array-song (in this case) to be removed, if nowplaying is present (artist, album name, song, etc).
Check the update, the code now removes the entire song if nowplaying is found. I'm guessing that there's only one nowplaying, and if I can guess again, it's the first element every time. If that's the case, just shift the array back once. (Which is the second solution)
This is my code updated, but I'm still getting the "nowplaying" object..: ffio.it/json.txt For the questions: IF nowplaying is present, it's always as first element (but it's not always present).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.