我正在尝试获得如下所示的输出。它将纬度和经度组合成对象内的数组。这就是我想让我做的事情。我是php和json的新手,并且一直在寻找没有运气的日子。可以从mysql数据库输出这个php。如果可以,你怎么做。
所需的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;
?>
答案 0 :(得分:0)
你可以这样做:
while ($row = $search_results->fetch_assoc())
{
$return_data[] = array(
'business_name' => $row['business_name'],
'latlng' => explode(',', $row['latlng']),
'business_id' => $row['business_id'],
'distance' => $row['distance']
);
}