2011-01-15 7 views
1

Ich möchte ein PHP JSON Daten foreach machen, aber ich habe ein Problem. Erstens: Ich kann den properties Teil nicht bekommen. Zweite alawys es in Zeile falsch zeigen: echo '<div class="image">... Fatal error: Cannot use object of type stdClass as array in ... Dies ist die JSON-Daten:PHP JSON Daten foreach Problem

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

Hier ist meine 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>'; 
} 
} 
+0

Können Sie die Ergebnisse eines var_dump ($ result) posten (wenn Sie nicht xdebug haben, sehen Sie sich die Quelle an, um die Zeilenumbrüche zu erhalten). – Naatan

Antwort

1

Wenn ich Sie wäre, würde ich zwingen, nur die Decodierung ein assoc array von true als der zweite arg zu json_decode. Wenn Sie kann nicht oder nicht wollen, dass tun, dann versuchen es so den Zugriff auf:

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

0

Verwenden json_decode($the_data, true); statt json_decode($the_data); auf diese Weise wird es Ihnen ein assoziatives Array zurück anstelle eines StdClass.