2
I have a Webservice in PHP that returns me a JSON with values from a mysql database.
I need that, in an Array, whenever there is a null value (when = null
), be replaced by white (= ""
).
I’m doing it this way, but without success.
<?php
header("Content-Type: application/json; charset=utf-8;");
include('connectdb.php');
$something = $_GET['cod'];
$sqlcode = mysql_query("Select descricao, cliente, local from terminal_cartao Where descricao='$something'");
$sqlcode2 = mysql_query("Select descricao, cliente, local from terminal_cartao");
$jsonObj= array();
if($something == 'all')
{
while($result=mysql_fetch_object($sqlcode2))
{
$jsonObj[] = $result;
}
}
else{
while($result=mysql_fetch_object($sqlcode))
{
$jsonObj[] = $result;
}
}
foreach ($jsonObj as $key => $value) {
if ($value === null) {
$jsonObj[$key] = "";
}
}
$final_res =json_encode($jsonObj);
echo $final_res;
exit;
did not work, edited my answer and put the complete code. What may be wrong?
– Rene Sá
I edited the answer now for two-dimensional array.
– Lucas
Cannot use Object of type stdClass as array in
$jsonObj[$k1][$k2] = "";
or en: Cannot use object of type stdClass as array in$jsonObj[$k1][$k2] = "";
– Rene Sá
I edited again, I didn’t see that they were objects.
– Lucas
Thank you, it worked perfectly!
– Rene Sá