2

I'm using The Echo Nest API to find similar artists. The response looks like:

{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"name": "Audio Adrenaline", "id": "ARGEZ5E1187FB56F38"}, {"name": "Tree63", "id": "ARWKO2O1187B9B5FA7"}]}}

How can I take the artists it results and put them into an array? So I can then echo them later like:

echo $artist[0];
1
  • 1
    json_decode() Commented Sep 10, 2013 at 18:59

3 Answers 3

6

You just need to use json_decode() with the second parameter set to TRUE.

$str = '...';
$json = json_decode($str, TRUE);
$artist = $json['response']['artists'];    
//$artist = json_decode($str, TRUE)['response']['artists']; as of PHP 5.4

print_r($artist);

Output:

Array
(
    [0] => Array
        (
            [name] => Audio Adrenaline
            [id] => ARGEZ5E1187FB56F38
        )

    [1] => Array
        (
            [name] => Tree63
            [id] => ARWKO2O1187B9B5FA7
        )

)

Codepad!

Sign up to request clarification or add additional context in comments.

Comments

2

json_decode() is what you need

$artist = json_decode($json);

or as an associative array

$artist = json_decode($json, true);

Comments

0

Use json_decode, found here.

Sample:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));

Output:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.