-1

Note that this is similar to Processing Multidimensional JSON Array with PHP, only with one caviot.

I have a JSON with a few arrays from the following taken from Airtable (URLs censored)

{
    "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.

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?

2
  • Cannot reproduce, 3v4l.org/lpJeb Commented Oct 26, 2020 at 9:01
  • Try to set second parameter of json_decode to true to get an associative array. It will be much easier for you. See php.net/manual/en/function.json-decode.php Commented Oct 26, 2020 at 9:20

1 Answer 1

0

It works.

Check if there are other errors.

Whether some expected structure does not exist in the real data.

% cat test4.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

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


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


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

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.