【问题标题】:How to retrieve yelp category_filter with PHP? A JSON object inside a JSON object如何使用 PHP 检索 yelp category_filter? JSON 对象内的 JSON 对象
【发布时间】:2013-04-09 19:57:00
【问题描述】:

我正在使用 Yelp API v1,我正在尝试获取存储在类别对象中的 category_filter。我的 JSON 的一小部分如下所示:

"businesses": [
{
 "address1": "800 N Point St",
 "phone": "4157492060",
 "categories": [{
"category_filter": "newamerican",
"search_url": "http://www.yelp.com/search?cflt=newamerican\u0026find_desc=\u0026find_loc=800+N+Point+St%2C+San+Francisco+94109",
"name": "American (New)"
}],
 "name": "Gary Danko",
 }]

我的 php 脚本如下所示:

<?php
$yelpstring = file_get_contents("PATH TO JSON", true); 

$obj = json_decode($yelpstring, true);
foreach($obj['businesses'] as $business){
echo "Restaurant name: " . $business['name']."<br/>";
echo "Restaurant Type: " . $business['categories']['category_filter']."<br/>";
echo "Address 1: " . $business['address1']."<br/>";
echo "Phone: " . $business['phone'] ."<br/>";
echo "<hr>";
}
?>

我得到了姓名、地址 1 和电话,但不是 category_filter 我得到一个 php 错误,上面写着:

“注意:未定义索引:file.php 第 12 行中的 category_filter”

我不明白我做错了什么。

【问题讨论】:

    标签: php json multidimensional-array yelp


    【解决方案1】:

    $business['categories']['category_filter']

    错了

    你必须使用:$business['categories'][0]['category_filter']

    在您的 json 中是“[{”,这意味着在另一个数组中有一个数组(/object,解码为 assoc-array)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多