开发者

PHP JSON data foreach problem

开发者 https://www.devze.com 2023-02-04 17:09 出处:网络
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\">...

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->attach开发者_如何学Goment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</a></div>'; 
}
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消