-1

我使用 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'];
}

但我没有得到任何结果。

4

1 回答 1

4

问题是您正在访问您$result的对象:

$result->FHRSEstablishment

但是当您将json_decode第二个参数设置为 调用时true,它会返回一个关联数组,您应该以如下方式访问它:

$result['FHRSEstablishment']['EstablishmentCollection'] //...

如果您希望能够$result使用对象表示法访问您的,您应该将其定义为:

$result = json_decode($trends) //without 2nd parameter = true
于 2012-05-28T13:31:09.970 回答