douwen5985 2016-03-09 21:40
浏览 71

在Tripadvisor JSON API文件中获取三个对象的PHP值

Been struggling with this for too long now, so am kindly asking for your help.

How can I, using PHP, get the values of the text fields in the reviews of which there are three in this JSON file below.

Want to use a foreach loop for this, thanks for helping me out!

{
  "address_obj": {
    "street1": "Rustenburgerstreet 384",
    "street2": null,
    "city": "Amsterdam",
    "state": "North Holland Province",
    "country": "The Netherlands",
    "postalcode": "1072 HG",
    "address_string": "Rustenburgerstreet 384, 1072 HG Amsterdam The Netherlands"
  },
  "percent_recommended": null,
  "latitude": "52.35162",
  "rating": "5.0",
  "attraction_types": [
    {
      "name": "concerts",
      "localized_name": "Concerts"
    },
    {
      "name": "blues bars",
      "localized_name": "Blues Bars"
    },
    {
      "name": "jazz bars",
      "localized_name": "Jazz Bars"
    },
    {
      "name": "bar/ clubs",
      "localized_name": "Bars & Clubs"
    }
  ],
  "wikipedia_info": null,
  "location_id": "3724036",
  "review_rating_count": {
    "1": "0",
    "2": "0",
    "3": "1",
    "4": "4",
    "5": "35"
  },
  "ranking_data": {
    "ranking_string": "#12 of 73 Theater & Concerts in Amsterdam",
    "ranking_out_of": "73",
    "geo_location_id": "188590",
    "ranking": "12",
    "geo_location_name": "Amsterdam"
  },
  "photo_count": "35",
  "location_string": "Amsterdam, North Holland Province",
  "trip_types": [
    {
      "name": "business",
      "value": "0",
      "localized_name": "Business"
    },
    {
      "name": "couples",
      "value": "8",
      "localized_name": "Couples"
    },
    {
      "name": "solo",
      "value": "7",
      "localized_name": "Solo travel"
    },
    {
      "name": "family",
      "value": "0",
      "localized_name": "Family"
    },
    {
      "name": "friends",
      "value": "21",
      "localized_name": "Friends getaway"
    }
  ],
  "web_url": "Attraction_Review-g188590-d3724036-Reviews-m34757-CC_Music_Cafe-Amsterdam_North_Holland_Province.html",
  "reviews": [
    {
      "id": "353301385",
      "lang": "en",
      "location_id": "3724036",
      "published_date": "2016-03-06T05:20:19-0500",
      "rating": 5,
      "helpful_votes": "0",
      "rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
      "url": "ShowUserReviews-g188590-d3724036-r353301385-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review353301385",
      "trip_type": "Solo travel",
      "travel_date": "2016-02",
      "text": "I am a regular visitor of CC Muziekcafé Amsterdam but have felt at home from the very first time. What I like about CC is the atmosphere where great music and hospitality are mixed in the best way...",
      "user": {
        "username": "Yvon H",
        "user_location": {
          "name": "Groningen Province, The Netherlands",
          "id": "188570"
        }
      },
      "title": "A great place to hear live music and meet all sorts of interesting people, both local and traveling",
      "is_machine_translated": false
    },
    {
      "id": "351658487",
      "lang": "en",
      "location_id": "3724036",
      "published_date": "2016-02-28T11:13:12-0500",
      "rating": 5,
      "helpful_votes": "0",
      "rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
      "url": "ShowUserReviews-g188590-d3724036-r351658487-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review351658487",
      "trip_type": "Friends getaway",
      "travel_date": "2016-02",
      "text": "4th time we have been here, another great night at the music cafe, friendly people and a barman who knows how to just put enough swear words in to sound cool",
      "user": {
        "username": "Francois S",
        "user_location": {
          "name": "Cardiff, United Kingdom",
          "id": "186460"
        }
      },
      "title": "Jazz funk Jam session night Thursday",
      "is_machine_translated": false
    },
    {
      "id": "350605184",
      "lang": "en",
      "location_id": "3724036",
      "published_date": "2016-02-24T10:18:57-0500",
      "rating": 5,
      "helpful_votes": "1",
      "rating_image_url": "img/cdsi/img2/ratings/traveler/s5.0-34757-5.png",
      "url": "ShowUserReviews-g188590-d3724036-r350605184-CC_Music_Cafe-Amsterdam_North_Holland_Province.html#review350605184",
      "trip_type": "Couples",
      "travel_date": "2015-09",
      "text": "CC muziekcafe is a very cosy place with excellent live music and interaction with the musicians. What really makes the place is the owner Rene who knows a lot about music and now and then even sings...",
      "user": {
        "username": "ImagineNL",
        "user_location": {
          "name": "Schagen, The Netherlands",
          "id": "609049"
        }
      },
      "title": "Cupid",
      "is_machine_translated": false
    }
  ],
  • 写回答

3条回答 默认 最新

  • doutang6819 2016-03-09 21:54
    关注

    It doesn't seem like you need a foreach to fetch the reviews element. Not sure if I understood your question correct, but did you want something like this:

    $assoc_json = json_decode($your_json, true);
    var_dump($assoc_json['reviews']);
    

    The above turns your json into an associative array, and just access the review element.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站