简体   繁体   中英

Handling Complex Arrays in PHP

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]);

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;

我更喜欢sf_admin的答案,因为这种杂乱的对象确实更适合作为对象,但是使用json_decode($response, true)那么我认为您应该能够这样访问它:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM