繁体   English   中英

无法使用json_decode访问数组元素

[英]Unable to access array element with json_decode

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

但我没有得到任何结果。

问题是您将$result作为对象访问:

$result->FHRSEstablishment

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

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

如果您希望能够使用对象符号访问$result ,则应将其定义为:

$result = json_decode($trends) //without 2nd parameter = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM