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

时间:2016-03-09 21:40:34

标签: php json api tripadvisor

现在已经挣扎太久了,所以请求你的帮助。

我如何使用PHP获取下面这个JSON文件中有三个文章的评论中的文本字段的值。

想要使用foreach循环,感谢帮助我!

{
  "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 个答案:

答案 0 :(得分:0)

您似乎不需要foreach来获取reviews元素。不确定我是否理解你的问题是正确的,但是你想要这样的事情:

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

上面将你的json变成一个关联数组,只需访问review元素。

答案 1 :(得分:0)

您可以使用此代码

<?php

$json = <<<EOF
{
  "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 &amp; 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
    }
  ]
}
EOF;

$array = json_decode($json, true);
$texts = array_map(
    function($item) {
        return $item['text'];
    }, $array['reviews']
);

答案 2 :(得分:0)

一种简单,天真的方法是:

{{1}}

或者你可以在foreach循环中做自己的东西。 This answer为您提供另一种方式(更优雅)来创建&#39;结果&#39;数组,但既然你要求一个foreach&#39;,我想我会加5美分。