我使用json_decode从JSON响应中获取数组:
$result = (json_decode($trends,true));
给了我以下内容(我没有包含所有的EstablishmentDetail数组结果)
Array ( [FHRSEstablishment] => Array ( [Header] => Array ( [#text] => [ExtractDate] => 2012-05-28 [ItemCount] => 5 [ReturnCode] => Success ) [EstablishmentCollection] => Array ( [EstablishmentDetail] => Array ( [0] => Array ( [FHRSID] => 248659 [LocalAuthorityBusinessID] => INS/06/06179 [BusinessName] => Ancient Raj [BusinessType] => Restaurant/Cafe/Canteen [BusinessTypeID] => 1 [AddressLine1] => 26 North Lane, Canterbury, [PostCode] => CT2 7EE [RatingValue] => 3 [RatingKey] => fhrs_3_en-GB [RatingDate] => 2010-11-18 [LocalAuthorityCode] => 180 [LocalAuthorityName] => Canterbury City [Scores] => [SchemeType] => FHRS [Geocode] => )
我认为我能够使用foreach来获取BusinessName:
foreach ($result->FHRSEstablishment->EstablishmentCollection->EstablishmentDetail as $detail){
echo $detail['BusinessName'];
}
但我没有得到任何结果。
答案 0 :(得分:4)
问题是您正在访问$result
作为对象:
$result->FHRSEstablishment
但是当您在第二个参数设置为json_decode
的情况下调用true
时,它会返回一个关联数组,您应该访问该数组:
$result['FHRSEstablishment']['EstablishmentCollection'] //...
如果您希望能够使用对象表示法访问$result
,则应将其定义为:
$result = json_decode($trends) //without 2nd parameter = true