简体   繁体   English

使用 PHP 在 JSON 嵌套数组中处理 JSON 嵌套数组中的项目

[英]Process item in a JSON nested array in a JSON nested array using PHP

Note that this is similar to Processing Multidimensional JSON Array with PHP , only with one caviot.请注意,这类似于Processing Multidimensional JSON Array with PHP ,只有一个 caviot。

I have a JSON with a few arrays from the following taken from Airtable (URLs censored)我有一个 JSON,其中有几个 arrays 来自 Airtable(经过审查的网址)

{
    "records": [
        {
            "id": "RECORD_ID",
            "fields": {
                "Name": "Setsuna Meioh",
                "Birthday": "2020-10-29",
                "Gender": "Female",
                "Series": "Sailor Moon",
                "Picture": [
                    {
                        "id": "this_is_id1",
                        "url": "http://IMAGE.URL",
                        "filename": "IMAGE_FILENAME",
                        "size": 97060,
                        "type": "image/png",
                        "thumbnails": {
                            "small": {
                                "url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
                                "width": 34,
                                "height": 36
                            },
                            "large": {
                                "url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
                                "width": 295,
                                "height": 317
                            },
                            "full": {
                                "url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
                                "width": 3000,
                                "height": 3000
                            }
                        }
                    }
                ]
            }
        },
        {
            "id": "RECORD_ID2",
            "fields": {
                "Name": "Rin Hoshizora",
                "Birthday": "2020-11-01",
                "Gender": "Female",
                "Series": "Love Live (School Idol Project)",
                "Picture": [
                    {
                        "id": "this_is_id2",
                        "url": "http://IMAGE.URL",
                        "filename": "IMAGE_FILENAME",
                        "size": 131749,
                        "type": "image/png",
                        "thumbnails": {
                            "small": {
                                "url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
                                "width": 34,
                                "height": 36
                            },
                            "large": {
                                "url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
                                "width": 295,
                                "height": 317
                            },
                            "full": {
                                "url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
                                "width": 3000,
                                "height": 3000
                            }
                        }
                    }
                ]
            }
        }
    ]
}

I'm trying to access the url item in a Picture array, and I already have the jsonDecode method already set up from a cURL response.我正在尝试访问图片数组中的url项目,并且我已经从 cURL 响应设置了jsonDecode方法。

Now, I already know how to use the brackets [] when it's accessing a array (as shown on the link above), but the problem is, there is another array in a array.现在,我已经知道如何在访问数组时使用括号[] (如上面的链接所示),但问题是,数组中还有另一个数组。

What I did was using a longer string to get to a array.我所做的是使用更长的字符串来获取数组。

echo $data->records[0]->fields->Picture[0]->thumbnails->small->url;

I tried that, but unfortunately came out nothing.我试过了,但不幸的是什么都没有。 Then I tried another method below然后我尝试了下面的另一种方法

$Image1Base = $data->records[0]->fields->Picture[0];
$Image1URL = $Image1Base->url;

// then the echo

echo $Image1URL

Came out nothing either.也什么都没出来。 Is there something missing?有什么东西不见了吗?

It works.有用。

Check if there are other errors.检查是否有其他错误。

Whether some expected structure does not exist in the real data.一些预期的结构是否在真实数据中不存在。

% cat test4.php %猫测试4.php

<?php
$jsonStr =
'{
    "records": [
        {
            "id": "RECORD_ID",
            "fields": {
                "Name": "Setsuna Meioh",
                "Birthday": "2020-10-29",
                "Gender": "Female",
                "Series": "Sailor Moon",
                "Picture": [
                    {
                        "id": "this_is_id1",
                        "url": "http://IMAGE.URL",
                        "filename": "IMAGE_FILENAME",
                        "size": 97060,
                        "type": "image/png",
                        "thumbnails": {
                            "small": {
                                "url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
                                "width": 34,
                                "height": 36
                            },
                            "large": {
                                "url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
                                "width": 295,
                                "height": 317
                            },
                            "full": {
                                "url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
                                "width": 3000,
                                "height": 3000
                            }
                        }
                    }
                ]
            }
        },
        {
            "id": "RECORD_ID2",
            "fields": {
                "Name": "Rin Hoshizora",
                "Birthday": "2020-11-01",
                "Gender": "Female",
                "Series": "Love Live (School Idol Project)",
                "Picture": [
                    {
                        "id": "this_is_id2",
                        "url": "http://IMAGE.URL",
                        "filename": "IMAGE_FILENAME",
                        "size": 131749,
                        "type": "image/png",
                        "thumbnails": {
                            "small": {
                                "url": "https://dl.airtable.com/THUMBNAIL_SMAL_URL",
                                "width": 34,
                                "height": 36
                            },
                            "large": {
                                "url": "https://dl.airtable.com/THUMBNAIL_LARGE_URL",
                                "width": 295,
                                "height": 317
                            },
                            "full": {
                                "url": "https://dl.airtable.com/THUMBNAIL_FULL_URL",
                                "width": 3000,
                                "height": 3000
                            }
                        }
                    }
                ]
            }
        }
    ]
}';

$data = json_decode($jsonStr);
$Image1Base = $data->records[0]->fields->Picture[0];
$Image1URL = $Image1Base->url;
var_dump($Image1URL);

% php -S localhost:8001 % php -S localhost:8001

view-source:http://localhost:8001/test4.php


string(16) "http://IMAGE.URL"


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

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