Search code examples
phpandroidreplaceall

need to add commas between double quotes php output


I use the output of PHP in an android application, the php code generates output like:

"x""y""z"

I want to separate between them using commas whether by changing the php code or using programming in android. to be

"x","y","z"

I tried by programming using ReplaceAll to replace every ("") by (",") but I can't put this in the syntax of command as it refuses to do """", "",""

My PHP Code:

<?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();    
}
?>

thanks


Solution

  • you want a comma? so just add one to the string output:

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

    you could concatenate the results and echo once:

    $out=array();
    while ($stmt->fetch()) 
    {
    $out[]=$Cabinet_Row);
    }
    $stmt->close();
    }
    
    echo '"'.implode('","',$out).'"';
    

    i see no real need for json_encode() considering your output.