Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access nested arrays from a json_decode in PHP?

Tags:

json

php

I have spent hours at this and I've read loads of answers here on stackoverflow and none have helped.

All I've been able to do so far is to print Market Square - Clifden for this example.

$obj = json_decode($data);
$obj = $obj[0];
print $obj->{'title'};

I can't figure out how to access "name" nested inside "image" so I can get market_square_clifden.jpg.

I'd be grateful for some pointers.

array(1){
        [0]=>array(12){
            ["_id"]=>array(1){
                ["$oid"]=>string(24)"51f674e4e4b0066cc8197033"
            }
            ["display"]=>int(1)
            ["title"]=>string(23)"Market Square - Clifden"
            ["class"]=>string(21)"market_square_clifden"
            ["icon"]=>string(16)"camera_small.png"
            ["image"]=>array(4){
                ["name"]=>string(25)"market_square_clifden.jpg"
                ["top"]=>string(16)"7.98958587646484"
                ["left"]=>string(18)"397.98614501953125"
                ["maxwidth"]=>string(16)"599.777777671814"
            }
            ["locs"]=>array(2){
                ["lng"]=>float(-10.022516)
                ["lat"]=>float(53.488111)
            }
            ["pov"]=>array(3){
                ["heading"]=>string(17)"-14.1950626239811"
                ["pitch"]=>string(18)"-6.368221166504443"
                ["zoom"]=>string(18)"0.8399999999999999"
            }
            ["photo"]=>array(3){
                ["takenby"]=>string(13)"Robert French"
                ["sentinby"]=>string(34)"The Lawrence Photograph Collection"
                ["description"]=>string(263)"Clifden (Irish: An Clochán, meaning 'stepping stones' is a town on the coast of County Galway, Ireland and being Connemara's largest town, it is often referred to as 'the Capital of Connemara'. It is located on the Owenglen River where it flows into Clifden Bay."
            }
            ["date"]=>array(2){
                ["posted"]=>string(53)"Mon Jul 29 2013 14:53:53 GMT+0100 (GMT Daylight Time)"
                ["circa"]=>string(9)"1880-1900"
            }
            ["comments"]=>array(1){
                [0]=>array(2){
                    ["poster"]=>string(0)""
                    ["comment"]=>string(0)""
                }
            }
            ["tags"]=>array(1){
                [0]=>array(2){
                    ["name"]=>string(0)""
                    ["slug"]=>string(0)""
                }
            }
        }
    }
like image 824
chris loughnane Avatar asked Mar 09 '14 04:03

chris loughnane


People also ask

How can I access and process nested objects arrays or JSON?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

What json_decode () function will return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

What is the use of json_decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

What is a nested JSON?

Nested JSON is a JSON file with a big portion of its values being other JSON objects. Compared with Simple JSON, Nested JSON provides higher clarity in that it decouples objects into different layers, making it easier to maintain.


1 Answers

As the commenters mentioned, please fix your variable formatting - it helps get answers more quickly.

Assuming you are using default json_decode settings, each JSON object will become a PHP object. (See the json_decode documentation at php.net.)

$obj = json_decode($data);
$obj = $obj[0];
// title attribute
$obj->title
// image object
$obj->image
// image name
$obj->image->name

If you force everything to be associative arrays:

$obj = json_decode($data,true);
$obj = $obj[0];
// title attribute
$obj['title']
// image object
$obj['image']
// image name
$obj['image']['name']
like image 193
dethtron5000 Avatar answered Oct 18 '22 17:10

dethtron5000