1

I want to make a PHP JSON data foreach, but I met some problem. First: I can not get the properties part. Second it alawys show wrong in line: echo '<div class="image">... Fatal error: Cannot use object of type stdClass as array in ... This is the json data:

[
    {
        "post_id": "504464716_189878284371815",
        "message": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
        "attachment": {
            "media": [
                {
                    "href": "http:\/\/www.facebook.com\/photo.php?fbid=493710409716&set=a.453260184716.254996.504464716",
                    "alt": "",
                    "type": "photo",
                    "src": "http:\/\/photos-f.ak.fbcdn.net\/hphotos-ak-snc4\/hs049.snc4\/34793_493710409716_504464716_5821684_2056840_s.jpg",
                    "photo": {
                        "aid": "2166659457206182932",
                        "pid": "2166659457211749620",
                        "owner": 504464716,
                        "index": 24,
                        "width": 225,
                        "height": 225
                    }
                }
            ],
            "name": "Wall Photos",
            "href": "http:\/\/www.facebook.com\/album.php?aid=254996&id=504464716",
            "caption": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
            "description": "",
            "properties": [
                {
                    "name": "By",
                    "text": "Suman Acharya",
                    "href": "http:\/\/www.facebook.com\/profile.php?id=504464716"
                }
            ],
            "icon": "http:\/\/static.ak.fbcdn.net\/rsrc.php\/yD\/r\/aS8ecmYRys0.gif",
            "fb_object_type": "album",
            "fb_object_id": "2166659457206182932"
        },
        "action_links": null,
        "privacy": {
            "value": ""
        }
    },
...
]

Here is my php code:

foreach ($data as $result) { 
echo '<div class="title"><a href="'.htmlspecialchars($result->link).'">'.htmlspecialchars($result->message).'<br />'.htmlspecialchars($result->description).'<br />'.htmlspecialchars($result->caption).'</a><br />';
if(!empty($result->attachment->properties[0]->text)){
    foreach ($result->attachment->properties[0] as $properties) { 
echo htmlspecialchars($properties->name).'<br /><a href="'.htmlspecialchars($properties->href).'">'.htmlspecialchars($properties->text).'</a></div>'; 
    }
}
if(!empty($result->attachment->media)){
echo '<div class="image"><a href="'.htmlspecialchars($result->attachment->media[0]->href).'"><img src="'.htmlspecialchars($result->attachment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</a></div>'; 
}
}
1
  • Can you post the results of a var_dump($result) (if you dont have xdebug, view source to get the line breaks). Commented Jan 15, 2011 at 15:50

2 Answers 2

1

If i were you i would just force the decoding to an assoc array by true as the second arg to json_decode. If you cant or dont want to do that then try accessing it like this:

$result->attachment->media->{0}->href

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

Comments

0

Use json_decode($the_data, true); instead of json_decode($the_data); that way it will return you an associative array instead of a StdClass.

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.