在PHP中将JSON中的字符串作为数组提取

时间:2014-10-26 03:45:51

标签: javascript php html json phrase

我有来自Google API的以下JSON,我想提取cse_image -> src并在PHP数组中将其用作arr[0]作为第一个,arr[1]作为第二个,依此类推。

{
 "kind": "customsearch#search",
 "url": {
  "type": "application/json",
  "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
 },
 "queries": {
  "nextPage": [
   {
    "title": "Google Custom Search - Gravity Falls",
    "totalResults": "13600",
    "searchTerms": "Gravity Falls",
    "count": 2,
    "startIndex": 4,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "005215763543692940433:2hdsc4__avm",
    "filter": "0",
    "disableCnTwTranslation": "disable"
   }
  ],
  "request": [
   {
    "title": "Google Custom Search - Gravity Falls",
    "totalResults": "13600",
    "searchTerms": "Gravity Falls",
    "count": 2,
    "startIndex": 2,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "005215763543692940433:2hdsc4__avm",
    "filter": "0",
    "disableCnTwTranslation": "disable"
   }
  ],
  "previousPage": [
   {
    "title": "Google Custom Search - Gravity Falls",
    "totalResults": "13600",
    "searchTerms": "Gravity Falls",
    "count": 2,
    "startIndex": 1,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "005215763543692940433:2hdsc4__avm",
    "filter": "0",
    "disableCnTwTranslation": "disable"
   }
  ]
 },
 "context": {
  "title": "Gravity Falls"
 },
 "searchInformation": {
  "searchTime": 0.269451,
  "formattedSearchTime": "0.27",
  "totalResults": "13600",
  "formattedTotalResults": "13,600"
 },
 "items": [
  {
   "kind": "customsearch#result",
   "title": "Gravity Falls Apparel - Gravity Falls Wiki",
   "htmlTitle": "\u003cb\u003eGravity Falls\u003c/b\u003e Apparel - \u003cb\u003eGravity Falls\u003c/b\u003e Wiki",
   "link": "http://gravityfalls.wikia.com/wiki/Gravity_Falls_Apparel",
   "displayLink": "gravityfalls.wikia.com",
   "snippet": "Gravity Falls apparel are officially sold clothes. On December 11, 2012 \nWeLoveFine.com released...",
   "htmlSnippet": "\u003cb\u003eGravity Falls\u003c/b\u003e apparel are officially sold clothes. On December 11, 2012 \u003cbr\u003e\nWeLoveFine.com released...",
   "cacheId": "6Uh7-hm1BKoJ",
   "formattedUrl": "gravityfalls.wikia.com/wiki/Gravity_Falls_Apparel",
   "htmlFormattedUrl": "\u003cb\u003egravityfalls\u003c/b\u003e.wikia.com/wiki/\u003cb\u003eGravity\u003c/b\u003e_\u003cb\u003eFalls\u003c/b\u003e_Apparel",
   "pagemap": {
    "cse_image": [
     {
      "src": "http://img3.wikia.nocookie.net/__cb20130410025818/gravityfalls/images/thumb/5/54/Welovefine_rainbow_gnome.jpg/500px-Welovefine_rainbow_gnome.jpg" //This as arr[0]
     }
    ],
    "cse_thumbnail": [
     {
      "width": "225",
      "height": "225",
      "src": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcS6kUI6yrCQkhX45BaaylTdDWb9tKvUf2OxwXJJv5NONxG1f0o09YerhB9j"
     }
    ],
    "metatags": [
     {
      "viewport": "width=device-width, initial-scale=1.0, user-scalable=yes",
      "fb:app_id": "112328095453510",
      "og:type": "article",
      "og:site_name": "Gravity Falls Wiki",
      "og:title": "Gravity Falls Apparel",
      "og:description": "Gravity Falls apparel are officially sold clothes. On December 11, 2012 WeLoveFine.com released the first official Gravity Falls merchandise. The same day they launched a Gravity Falls T-shirt design contest which was judged by Alex Hirsch and Michael Rianda. The Grand prize winner received up to $2,000, with several runners up receiving various prizes on top of their own designs becoming part of the online store.",
      "og:url": "http://gravityfalls.wikia.com/wiki/Gravity_Falls_Apparel",
      "og:image": "http://img3.wikia.nocookie.net/__cb20130410025818/gravityfalls/images/thumb/5/54/Welovefine_rainbow_gnome.jpg/500px-Welovefine_rainbow_gnome.jpg",
      "apple-itunes-app": "app-id=623705389"
     }
    ]
   }
  },
  {
   "kind": "customsearch#result",
   "title": "Gravity Falls, Oregon - Gravity Falls Wiki",
   "htmlTitle": "\u003cb\u003eGravity Falls\u003c/b\u003e, Oregon - \u003cb\u003eGravity Falls\u003c/b\u003e Wiki",
   "link": "http://gravityfalls.wikia.com/wiki/Gravity_Falls,_Oregon",
   "displayLink": "gravityfalls.wikia.com",
   "snippet": "Gravity Falls, Oregon is a mysterious, sleepy, small town in Central Oregon, \nwhere there are many supernatural occurrences. It was founded by Quentin ...",
   "htmlSnippet": "\u003cb\u003eGravity Falls\u003c/b\u003e, Oregon is a mysterious, sleepy, small town in Central Oregon, \u003cbr\u003e\nwhere there are many supernatural occurrences. It was founded by Quentin ...",
   "cacheId": "le0YqUje3GYJ",
   "formattedUrl": "gravityfalls.wikia.com/wiki/Gravity_Falls,_Oregon",
   "htmlFormattedUrl": "\u003cb\u003egravityfalls\u003c/b\u003e.wikia.com/wiki/\u003cb\u003eGravity\u003c/b\u003e_\u003cb\u003eFalls\u003c/b\u003e,_Oregon",
   "pagemap": {
    "cse_image": [
     {
      "src": "http://img2.wikia.nocookie.net/__cb20120526133929/gravityfalls/images/thumb/f/fd/S1e1_gravity_falls_oregon_map.jpg/500px-S1e1_gravity_falls_oregon_map.jpg"  // This as arr[1]
     }
    ],
    "videoobject": [
     {
      "thumbnail": "http://img2.wikia.nocookie.net/__cb20140905002344/video151/images/thumb/e/ec/Gravity_Falls_-_Referencias_Interesantes/300px-Gravity_Falls_-_Referencias_Interesantes.jpg",
      "duration": "01:47"
     }
    ],
    "cse_thumbnail": [
     {
      "width": "299",
      "height": "168",
      "src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTEsgxZQQYbR400fXDcbDafk6y5Jr9wLjy7ZAV7F3sfBccdhkGnwfiNlPA"
     }
    ],
    "metatags": [
     {
      "viewport": "width=device-width, initial-scale=1.0, user-scalable=yes",
      "fb:app_id": "112328095453510",
      "og:type": "article",
      "og:site_name": "Gravity Falls Wiki",
      "og:title": "Gravity Falls, Oregon",
      "og:description": "Gravity Falls, Oregon is a mysterious, sleepy, small town in Central Oregon, where there are many supernatural occurrences. It was founded by Quentin Trembley, It's where Stan Pines lives and runs the Mystery Shack, a tourist trap which overcharges unlucky visitors for a glimpse at the world's most bizarre museum. Twin siblings Dipper and Mabel Pines are sent to stay with Stan for the summer, which leads them to discover the different yet curious wonders of Gravity Falls. Gravity Falls...",
      "og:url": "http://gravityfalls.wikia.com/wiki/Gravity_Falls,_Oregon",
      "og:image": "http://img2.wikia.nocookie.net/__cb20120526133929/gravityfalls/images/thumb/f/fd/S1e1_gravity_falls_oregon_map.jpg/500px-S1e1_gravity_falls_oregon_map.jpg",
      "apple-itunes-app": "app-id=623705389"
     }
    ]
   }
  }
 ]
}

语言:PHP

我想使用PHP提取JSON,然后使用

访问它

arr[0]http://img3.wikia.nocookie.net/__cb20130410025818/gravityfalls/images/thumb/5/54/Welovefine_rainbow_gnome.jpg/500px-Welovefine_rainbow_gnome.jpg

arr[1]http://img2.wikia.nocookie.net/__cb20120526133929/gravityfalls/images/thumb/f/fd/S1e1_gravity_falls_oregon_map.jpg/500px-S1e1_gravity_falls_oregon_map.jpg

如何使用json_decode或任何其他类似方法执行此操作

1 个答案:

答案 0 :(得分:1)

$data = json_decode($string, true);
echo $data['items'][0]['pagemap']['cse_image'][0]['src'];
echo $data['items'][1]['pagemap']['cse_image'][0]['src'];