Last active
March 31, 2021 18:53
-
-
Save tureki/7af3ea2554eba60c34c5 to your computer and use it in GitHub Desktop.
json_encode null to empty string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$value = array( | |
"deep"=>1, | |
"data"=>null, | |
"node"=>array( | |
"deep"=>2, | |
"data"=>null, | |
"node"=>array( | |
"deep"=>3 | |
) | |
), | |
null | |
); | |
function convert_before_json(&$item, $key) | |
{ | |
$item = utf8_encode($item); | |
} | |
array_walk_recursive($value, "convert_before_json"); | |
echo json_encode($data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$value = array( | |
"deep"=>1, | |
"data"=>null, | |
"node"=>array( | |
"deep"=>2, | |
"data"=>null, | |
"node"=>array( | |
"deep"=>3 | |
) | |
), | |
null | |
); | |
array_walk_recursive($value, function (&$item, $key) { | |
$item = null === $item ? '' : $item; | |
}); | |
echo json_encode($value); |
Thank you, this was driving me crazy!
thankks a lot
thank its help me a lot !!!
I got an error if the collection didn't have a null value. how can i handle it ? thank you !!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks ! <3