简体   繁体   English

在PHP中处理复杂数组

[英]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. 我过去使用过PHP处理数组,但是通常使用简单的关联数组,易于管理和抓取。

I am making an HTTP POST request to an API endpoint that returns a lot of data in JSON format. 我正在向API端点发出HTTP POST请求,该请求以JSON格式返回大量数据。 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. 我正在使用json_decode($ response,true)将json转换为数组,并且尝试访问数组的组件时不走运-只为我提供了一个空白页。 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. 另外,如果我在数组上执行print_r,我只会得到一堆数据,所以我至少知道API正在返回数据。

Here's a snippet of the response from the API 这是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? 假设我将如何使用PHP访问track对象下的NAME值? (In this example I'd be trying to return the value "Some Song Name 123") (在此示例中,我将尝试返回值“ 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 某些歌曲名称123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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