簡體   English   中英

如何從MYSQL數據庫創建多維JSON數組?

[英]How to create multidimensional JSON Array from MYSQL database?

我有以下代碼,這些代碼將從mysql數據庫中打印一些JSON。

但是,當我檢查JSON輸出時,JSON無效。

這是我的PHP頁面上顯示的JSON:

[{
    "id": "1",
    "title": "test title",
    "about": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "address": "some address goes here",
    "lat": "51",
    "lon": "0.888",
    "distance": {
        "miles": 3.973345345,
        "kilometers": 6.39345348
    }
}][{
    "id": "3",
    "title": "test title 5",
    "about": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "address": "some address goes here",
    "lat": "51",
    "lon": "0.256",
    "distance": {
        "miles": 3.9735000071413,
        "kilometers": 6.3947283954928
    }
}]

這是我的PHP代碼:

header('Content-type: application/json');

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {    
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos (deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$kilometers = $miles * 1.609344;
return compact('miles','kilometers'); 
}

$records = array();


/* soak in the passed variable or set our own */
$latitude2 = floatval($_GET['latitude']); //no default
$longitude2 = floatval($_GET['longitude']); //no default


/* grab the posts from the db */
   $sql = "SELECT * FROM businesses ORDER BY id";
   $query = mysqli_query($db_conx, $sql);
   $productCount = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
{

extract($row);


$latitude = $row['lat'];    
$longitude = $row['lon'];


$point1 = array('lat' => number_format ($latitude,4,'.',''), 'long' => number_format ($longitude,4,'.',''));
$point2 = array('lat' => number_format ($latitude2,4,'.',''), 'long' => number_format ($longitude2,4,'.',''));
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);


$channel = array(
'id' => $id,
'title' => $title,
'about' => $about,
'address' => $address,
'lat' => $latitude,
'lon' => $longitude,
'distance' => $distance,
);
}   
$channels = array($channel);
$records[] = $channel;
//$json = json_encode($channel);
//echo $json;

echo '' . json_encode($records, JSON_UNESCAPED_SLASHES) . '';
}      

如果我在數據庫中只有一條記錄,它可以正常工作,但是當它的記錄超過1條時,JSON輸出無效。

有人可以就這個問題提出建議嗎?

這里應該是這樣的:

while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    // code without `extract`:

    $latitude = $row['lat'];    
    $longitude = $row['lon'];

    $point1 = array('lat' => number_format ($latitude,4,'.',''), 'long' => number_format ($longitude,4,'.',''));
    $point2 = array('lat' => number_format ($latitude2,4,'.',''), 'long' => number_format ($longitude2,4,'.',''));
    $distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);

    $channel = array(
        'id' => $row['id'],
        'title' => $row['title'],
        'about' => $row['about'],
        'address' => $row['address'],
        'lat' => $latitude,
        'lon' => $longitude,
        'distance' => $distance,
    );

    $records[] = $channel;
}
// echo ONCE
// and as `json_encode` returns a string - using '' is USELESS
echo json_encode($records, JSON_UNESCAPED_SLASHES);

另外,由於$point2 永不更改 ,因此最好設置

$point2 = array('lat' => number_format ($latitude2,4,'.',''), 'long' => number_format ($longitude2,4,'.',''));

while循環之外

暫無
暫無

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

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