0

I feel like i am slightly insane, and I have certainly read the docs on this. I am completely unable to echo out various objects in a JSON array in PHP. I am not sure what I'm doing wrong, but I'm ripping my hair out...

Here is my JSON array:

{
    "photos": {
        "page": 1,
        "pages": 1569045,
        "perpage": 1,
        "total": "1569045",
        "photo": [
            {
                "id": "14842817422",
                "owner": "23432140@N06",
                "secret": "c37cfa1914",
                "server": "3864",
                "farm": 4,
                "title": "pizza",
                "ispublic": 1,
                "isfriend": 0,
                "isfamily": 0
            }
        ]
    },
    "stat": "ok"
}

I know this is simple, but I can't get it right. I would like to echo out four different values.

This is what I have been trying:

$photoId = $jsonDecoded['photos']['photo'][0]['id'];
$photoSecret = $jsonDecoded['photos']['photo'][0]['secret'];
$photoServer = $jsonDecoded['photos']['photo'][0]['server'];
$photoFarm = $jsonDecoded['photos']['photo'][0]['farm'];

I know this seems newbie. Please help...

Best,

5
  • are there multiple photo sub-arrays ? Commented Aug 6, 2014 at 11:17
  • possible duplicate of PHP encode JSON (2 dimensional array) Commented Aug 6, 2014 at 11:18
  • @AMB it just looks like that one. I've tried removing the single quotes, and I've tried doing it in object format with "->" Commented Aug 6, 2014 at 11:20
  • first do a json decode and store the values in $jsonDecode as jsonDecode = json_decode('json data',true) then your code should work. Commented Aug 6, 2014 at 11:20
  • you can add, true as a second parameter while doing json_deocode so it will create an array, Commented Aug 6, 2014 at 11:21

2 Answers 2

1

The problem is that you have both objects and arrays in your json, but are using array syntax in your php.

There are two ways to fix this, 1st simply set the second parameter of json_decode to true:

json_decode($json, true);

This will create a multidimentional array you can access as suggested in your question, eg:

$photoId = $jsonDecoded['photos']['photo'][0]['id'];

Alertinitivly you can use object property syntax on your existing $jsonDecoded:

$photoId = $jsonDecoded->photos->photo[0]->id;
Sign up to request clarification or add additional context in comments.

Comments

1

if there are multiple photo sub arrays then you can do like this.

//this will create array instead of object
$jsonDecoded = json_decode($your_feed_data,true);

foreach($jsonDecoded['photos']['photo'] as $sub_array){

$photoId = $sub_array['id'];
$photoSecret = $sub_array['secret'];
$photoServer = $sub_array['server'];
$photoFarm = $sub_array['farm'];

}

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.