Remove commas from json_encode

Issue

So, I want to remove the commas from between results.
The reason I want to do this is because later in the process I export to CSV and the commas are making my tables go crazy.

Here is my HTML

<input type="checkbox" name="piden[]" value="lunes">Lunes<br>
<input type="checkbox" name="piden[]" value="martes">Martes<br>
<input type="checkbox" name="piden[]" value="miercoles">Miércoles<br>
<input type="checkbox" name="piden[]" value="jueves">Jueves<br>
<input type="checkbox" name="piden[]" value="viernes">Viernes<br>
<input type="checkbox" name="piden[]" value="sabado">Sábado<br>
<input type="checkbox" name="piden[]" value="domingo">Domingo<br>

Here is my PHP

$piden =json_encode($_POST['piden']);

And my results on the Database looks like this.
The only problem are the commas in between results.

["miercoles","sabado","domingo"]

I could not find any similar questions.

Solution

If you want to remove commas then why are you using json_encode()?

After comma removal the strings cannot be decoded anymore. Use a different encoding, for example, join the values using a separator:

$piden = implode('/', $_POST['piden']);

However, even this way you may have issues when the input data contains commas. Most probably you are “encoding” the CSV by hand and fail to properly quote the values that contain the separator (commas f.e).

The PHP function fputcsv() can do all the hard work for you when it comes to export data as CSV.

Answered By – axiac

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published