0

I am working with json in Codeigniter and i have some fields which outputs null values i want to change those null values to empty string. I don't want to check every fields one by one.My CI code is like this:

$result= $this->get_model->get_model();
header("content-type: application/json");
$data[] = array('name'=>"result",'data'=>$result);
echo json_encode(array('success'=> 1,'posts'=>$data));
return $result; 

enter image description here

4
  • once you convert into the the string. Replacae null with empty string Commented Nov 17, 2015 at 6:09
  • Can u please explain it with example or give me a link for that Commented Nov 17, 2015 at 6:11
  • can you let me know what output echo json_encode(array('success'=> 1,'posts'=>$data)); this line prints. Commented Nov 17, 2015 at 6:14
  • this is the code snippet : "name":"latest", "data":[ { "timestamp":"2015-11-16 11:34:03", "avg_rating":null } ] Commented Nov 17, 2015 at 6:17

2 Answers 2

0

Try this code. It prints json replacing null values with empty

$result= $this->get_model->get_model();
header("content-type: application/json");
$data[] = array('name'=>"result",'data'=>$result);
$data_result = json_encode(array('success'=> 1,'posts'=>$data));

echo str_replace("null","",$data_result);

return $result; 
Sign up to request clarification or add additional context in comments.

8 Comments

I tried the above code but the same null values are coming.
what my code echo prints, please let me know and what is the value of $data_result
print_r($data_result); it outputs nothing this means there is no value in $data_result.
$data_result is a string. It is not an array. So, you should use echo
@sandeepsue- I am getting error in json i have uploaded the image where i am getting it.All avg_rating are changing like the above image.
|
0

This is the easiest way:

array_walk_recursive($array,function(&$item){$item=strval($item);});

From: https://developertipsandtricks.blogspot.com/2013/10/convert-null-to-empty-string-for-json.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.