1

I've read through many similar posts but I can't seem to get it to work right. I can parse and the whole json file, it's a chart from Shazam. I'm stuck on selecting nested keys and get their values. How can I get only the artist, title and numberOfShazams values into attributes?

Here is my code so far:

<?php
$json = file_get_contents('http://cdn.shazam.com/shazam/v2/en/MX/android/-/tracks/web_chart_world');
$json_data = json_decode($json,true);
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:<br>";
    } else {
        echo "$key => $val<br>";
    }
}
?>

1 Answer 1

1

I hope I understand you correctly, and you wanted the tracktitle, trackartist, and numberOfShazams. This works for me:

<?php
$json = file_get_contents('http://cdn.shazam.com/shazam/v2/en/MX/android/-/tracks/web_chart_world');
$json_data = json_decode($json,true);

foreach( $json_data['chart'] as $chart )
{
    echo '<p>';
    echo urldecode($chart['urlparams']['{tracktitle}']);
    echo '<br />';
    echo urldecode($chart['urlparams']['{trackartist}']);
    echo '<br />';
    echo $chart['properties']['numberOfShazams'];
    echo '</p>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, exactly what I was looking for, thank you very much!

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.