需要在双引号之间加逗号 php 输出

need to add commas between double quotes php output

我在 android 应用程序中使用 PHP 的输出,php 代码生成如下输出:

"x""y""z"

我想通过更改 php 代码或使用 android 中的编程来使用逗号分隔它们。 成为

"x","y","z"

我尝试通过编程使用 ReplaceAll 将每个 ("") 替换为 ("),但我无法将其放入命令语法中,因为它拒绝执行 """"、""," “

我的PHP代码:

<?php
$mysqli = new mysqli("localhost", "root", "", "ESM");
$coresite = $_POST["selectedcoresite"];
if($stmt = $mysqli->prepare("SELECT DISTINCT Cabinet_Row FROM cabinets WHERE Core_Site=?"))
{
$stmt->bind_param("s", $coresite);
$stmt->execute();
$stmt->bind_result($Cabinet_Row);
while ($stmt->fetch()) 
{
echo json_encode($Cabinet_Row);
}
$stmt->close();
}
else{
$mysqli->close();    
}
?>

谢谢

你想要一个逗号?所以只需在字符串输出中添加一个:

echo json_encode($Cabinet_Row).','; //leaves a final comma

您可以连接结果并回显一次:

$out=array();
while ($stmt->fetch()) 
{
$out[]=$Cabinet_Row);
}
$stmt->close();
}

echo '"'.implode('","',$out).'"';

考虑到您的输出,我认为 json_encode() 没有真正的必要。