我的while循环有问题,每个项目都需要一个逗号,但最后一个不需要一个,但我不知道我需要做什么......
while($row = $result->fetch_assoc()) {
echo "
{
id: " . $row['id'] . ",
dexNr: " . $row['mon_id'] . ",
name: '" . $row['mon_id'] . "',
expiration: " . $row['expire_timestamp'] . ",
lat: " . $row['lat'] . ",
lng: " . $row['lon'] . "
},
";
}
答案 0 :(得分:1)
您可以将每个项目推送到一个数组中,然后用逗号将其内爆。这将只在项目之间放置逗号,但不在末尾:
$output = array();
while($row = $result->fetch_assoc()) {
array_push($output, "
{
id: " . $row['id'] . ",
dexNr: " . $row['mon_id'] . ",
name: '" . $row['mon_id'] . "',
expiration: " . $row['expire_timestamp'] . ",
lat: " . $row['lat'] . ",
lng: " . $row['lon'] . "
}
");
}
$output = implode(",", $output);