簡體   English   中英

我想顯示一個api的所有結果

[英]I want to show all result from an api

我正在使用Google Places API,並且顯示了來自api的內容,但像這樣一個接一個地顯示

<?php
$str = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4');

$maps_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4';
$maps_json = file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);
$lat = $maps_array['results'][1]['name'];
$lat2 = $maps_array['results'][2]['name'];
echo $lat;
echo "<br>";
echo $lat2;


?>

但我想一次循環顯示所有結果

嘗試

foreach ($maps_array['results'] as $map) {
   echo $map['name'];
   echo "<br>";
}

將結果數組放在foreach並從中進行循環。 這樣,您將獲取所有數據,而不是將數據一乘一。對於數字數組,您可以添加++語句(不需要)。

希望這可以幫助。 祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM