我试图在下面得到这样的输出。它将纬度和经度组合成对象内部的一个数组。这就是我想让我做的事情。我对 php 和 json 很陌生,并且一直在寻找它好几天都没有运气。php可以从mysql数据库中输出这个吗?如果可以,你是怎么做到的。
所需的 Json 输出
[
{
"business_name": "Johns Pizza",
"latlng": [
99.457434,
14.349869
],
"business_id": "1"
},
{
"business_name": "Daves Pizza",
"latlng": [
19.457434,
13.349869
],
"business_id": "2"
},
{
"business_name": "Jakes Pizza",
"latlng": [
75.457434,
12.349869
],
"business_id": "3"
}
]
当前 Json 输出
[{"business_name":"Crush Wine Bar","latlng":"43.6894658949280400,-116.3533687591552700",
"business_id":"2","distance":"23.0298400562551"},{"business_name":"Apple Headquarters","latlng":"37.3324083200000000,-122.0304781500000000",
"business_id":"3","distance":"23.0298400562551"}]
我的.php文件
<?php
if (isset($_GET['lat']) && isset($_GET['lon'])&& isset($_GET['dist'])) {
//make a simple database connection
$db = new mysqli();
$lat = $_GET['lat'];
$lon = $_GET['lon'];
$dist = $_GET['dist'];
$search_sql = "SELECT business_id, business_name, CONCAT_WS(\",\",business_lat, business_lon) AS latlng, ( 3959 * acos( cos( radians(37) ) * cos( radians( " . $lat . " ) ) * cos( radians( " . $lon . " ) - radians(-122) ) + sin( radians(37) ) * sin( radians( " . $lat . " ) ) ) ) AS distance
FROM business_data HAVING distance < " . $dist . " ORDER BY distance LIMIT 0 , 20";
$search_results = $db->query($search_sql);
if($search_results->num_rows) {
while ($row = $search_results->fetch_assoc()) {
$return_data[] = array (
'business_name' => $row['business_name'],
'latlng' => $row['latlng'],
'business_id' => $row['business_id'],
'distance' => $row['distance']
);
} //end while
} else {
$return_data[] = array();
}//end if
} else {
$return_data[] = array();
}
$bd_json = json_encode($return_data);
$db->close();
echo $bd_json;
?>