代码:
while($row = mysqli_fetch_assoc($result))
{
$data[] = array(
'city' => $row["city"],
'hotel' => $row["hotel_name"]
);
}
mysqli_close($con);
$results = json_encode($data);
$json = json_decode($results, true);
foreach($json as $fet)
{
$datas = $fet['city']." | ".$fet['hotel'].", ";
echo $datas;
}在这段代码中,我创建了一个自动完成文本框,我希望在数据前后添加",我的数据如下所示:
delhi | Radisson, noida | Ramada Phuket Deevana Patong, noida | Eco Poplar但我想这样:
"delhi | Radisson", "noida | Ramada Phuket Deevana Patong", "noida | Eco Poplar"那我该怎么做呢?谢谢
发布于 2018-08-10 08:14:28
只需在单引号之间加上双引号:'"'
改进:您可以在获取结果的同时保存格式化的字符串。与JSON相关的函数是无用的。然后使用implode()输出
$data = [];
while($row = mysqli_fetch_assoc($result))
{
// double quotes between single quotes
$data[] = '"' . $row["city"] . ' | ' . $row["hotel_name"] . '"';
}
mysqli_close($con);
// Here I removed useless json_encode/decode functions
// Then output strings separated by a coma
echo implode(', ', $data);发布于 2018-08-10 08:16:08
在您的示例中,最后一个元素后面有逗号,请尝试以下代码:
$data = [];
foreach($json as $fet) {
$data[] = "\"{$fet['city']} | {$fet['hotel']}\"";
}
echo implode(", ", $data);内爆(): http://php.net/manual/en/function.implode.php
发布于 2018-08-10 08:14:25
$data[] = array(
'city' => $row["city"],
'hotel' => $row["hotel_name"]
);
$results = json_encode($data);
$json = json_decode($results, true);
foreach($json as $fet)
{
$datas = '"'.$fet['city']." | ".$fet['hotel'].'"';
echo $datas;
}https://stackoverflow.com/questions/51781728
复制相似问题