0

以下のような出力を得ようとしています。オブジェクト内の配列に緯度と経度を組み合わせます。それが私がやらせようとしていることです。私は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;
?>
4

1 に答える 1