PHP JSON veri foreach sorun

0 Cevap php

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": ""
        }
    },
...
]

İşte benim php kodu:

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>'; 
}
}

0 Cevap