3

Is it possible for the PHP json_encode function do not convert empty string values to null?

UPDATE
I can not replicate this behaviour in clear conditions, and looks like it's already a default for this function.

4
  • 4
    So far as I can see it doesn't convert empty string to null. Do you have an example??? Commented Apr 14, 2016 at 17:28
  • 3
    You're better to show your code (which you should do by default here on Stack Overflow). Commented Apr 14, 2016 at 17:33
  • one guy's null is another guy's empty string ? show the code please. Commented Apr 14, 2016 at 17:34
  • Yep, @DawidFerenczy without it looks like opinions divided )) Commented Apr 14, 2016 at 17:37

2 Answers 2

3

This is already the default behavior.

json_encode(['test' => '']);

generates:

{"test":""}
Sign up to request clarification or add additional context in comments.

Comments

1

No that i know of, but you could do this:

array_walk_recursive($value, function (&$item, $key) {
    $item = null === $item ? '' : $item;
});

1 Comment

Yes I also saw this kind of code here developertipsandtricks.blogspot.com/2013/10/… and it looks like eclipse of the mind, because I can't replicate this now in clear conditions )) So thank you but... Should I close this question?

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.