1

I've worked with arrays in the past with PHP, but generally simple associative arrays that are easy to manage and crawl.

I am making an HTTP POST request to an API endpoint that returns a lot of data in JSON format. I'm using json_decode($response, true) to convert the json into an array, and am trying to access components of the array without luck - just serving me a blank page. Alternatively, if I do print_r on the array I just get a jumble of data, so I know at least the API is returning data.

Here's a snippet of the response from the API

{
"data": {
    "accounts": [
        {
            "locations": [
                {
                    "name": "Test Location",
                    "soundZones": [
                        {
                            "name": "Main",
                            "nowPlaying": {
                                "startedAt": "2017-09-06T00:38:51.000Z",
                                "track": {
                                    "name": "Some Song Name 123",
                                    "imageUrl": "https://some-cdn-url.com",
                                    "Uri": "5hXEcqQhEjfZdbIZLO8mf2",
                                    "durationMs": 327000,
                                    "artists": [
                                        {
                                            "name": "SomeName",
                                            "Uri": "5lpH0xAS4fVfLkACg9DAuM"
                                        }
                                    ]
                                }
                            }
                        }
                    ]
                },

How would I use PHP to access, let's say, the NAME value under the track object? (In this example I'd be trying to return the value "Some Song Name 123")

Here's the code I'm using.. I know I'm way off

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {

$response = json_decode($response, true);

print_r($response[0]);

3 Answers 3

2

That's because you're being returned not just an array, but both an array and an object.

<?php

    echo $response[0]->data->accounts[0]['locations'][0]->soundZones[0]->nowPlaying->track->name;
Sign up to request clarification or add additional context in comments.

2 Comments

sf_admin's answer is good because it would make it unnecessary to convert the json to an array.
Thanks, this works exactly like I hoped. I appreciate the help
2

I like sf_admin's answer better, because this kind of messy object really lends itself more to being an object, but using json_decode($response, true) then I think you should be able to access it like this:

echo $response[0]['data']['accounts'][0]['locations'][0]['soundZones'][0][0]['nowPlaying']['track']['name'];

1 Comment

This works, but you're right I think sf_admin's version does it better. Thanks so much for the help
1

Try this.

//$response = json_decode($response, true);
$response = json_decode($response);

// loop
if (isset($response->data->accounts)) {
    foreach ($response->data->accounts as $account) {
        foreach ($account->locations as $location) {
            foreach ($location->soundZones as $soundZone) {
                print_r($soundZone->nowPlaying->track->name);
            }
        }
    }
}

// first
if (isset($response->data->accounts[0]->locations[0]->soundZones[0]->nowPlaying->track->name)) {
    print_r($response->data->accounts[0]->locations[0]->soundZones[0]->nowPlaying->track->name);
}

Some Song Name 123

Comments

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.