首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用foreach遍历JSON

使用foreach遍历JSON
EN

Stack Overflow用户
提问于 2018-05-31 01:25:21
回答 2查看 413关注 0票数 0

我一直在遵循这个指南,使用两个不同的app创建一个应用程序,但该指南太旧了,因此其中一个app的工作方式与指南中的不同。我试图从谷歌地理编码API中获取坐标,并将其粘贴到Web的位置中。我是PHP新手,所以我按照指南中的示例遍历了一个JSON对象,但我整晚都在尝试让它正常工作。这是来自place search API的JSON对象

代码语言:javascript
运行
复制
{  
"html_attributions":[  ],
"results":[  
  {  
     "geometry":{  },
     "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
     "id":"d4b0fb0f7bf5b2ea7df896a0c120a68efae039cf",
     "name":"Guadalajara Mexican Grill & Cantina",
     "opening_hours":{  },
     "photos":[  
        {  
           "height":2952,
           "html_attributions":[  ],
           "photo_reference":"CmRaAAAAfO4JKUaO8vCFM2dcu5LMu4mA4_HXQGJ1FyAnyJUre_kD6VOWiQj7tBEECx4AAct5AORIKipSYWg-Zprjlf8o-SFd7mBRGMXMVMwodFZ5KMLwPYPUhBnTTehGPkb9275pEhCkAqMwfmK29vYenk1wdwFvGhSIHR8ch6FONc99tGn4rVnesbuteg",
           "width":5248
        }
     ],
     "place_id":"ChIJ27es4SWa3IARcvjmt3xL2Aw",
     "price_level":2,
     "rating":4.4,
     "reference":"CmRRAAAA7Rx-l7juDX-1or5dfpK6qFcZ0trZ9cUNEUtKP2ziqHb2MhOE6egs-msJ2OdFKEuHhuNe-3Yk6yxUYwxCBqhDT3ci8pYZI4xYhPGyyDgDenbEU_8k84JiEtCGvj4bdIR0EhDR2Pqte5_kDUcCC9PJFVknGhQomvD4d7NBIhCFxI4i2iEc0w9UiA",
     "scope":"GOOGLE",
     "types":[  ],
     "vicinity":"105 North Main Street, Lake Elsinore"
  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  }
],
"status":"OK"
}

我正在尝试将所有的照片引用抓取到一个数组中,然后将它们插入google的Place Photos API中。这是我在这方面的尝试:

更新

代码语言:javascript
运行
复制
<?php 
if(!empty($_GET["location"])){
    //$API_key = "";
    $maps_url = 'https://' .
    'maps.googleapis.com/' .
    'maps/api/geocode/json' .
    '?address=' . urlencode($_GET['location']) .
    '&key=';



    $maps_json = file_get_contents($maps_url);
    $maps_array = json_decode($maps_json, true);

    $lat = $maps_array['results'][0]['geometry']['location']['lat'];
    $lng = $maps_array['results'][0]['geometry']['location']['lng'];

    $places_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' .
    'location=$lat,$lng' .
    '&radius=1500' .
    '&rankby=distance' .
    '&key=';

    $places_json = file_get_contents($places_url);
    $places_array = json_decode($places_json, true);

    if (!empty($places_array)) {
    foreach ($places_array as $item) {
        var_dump($places_array );
    }
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
 <title>What is Here?</title>
  </head>
  <body>
 <h1>Type in a location</h1>
 <p>This program will display pictures of places to go in that area</p>

  <form action ="">
  <input type ="text" name ="location"/>
  <button type ="submit">Go!</button>
  </form>
  <br/>
  <?php
    echo "$lat $lng";
  ?>

似乎不能让foreach循环做任何事情

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50610394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档