Skip to content

Instantly share code, notes, and snippets.

@tureki
Last active March 31, 2021 18:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tureki/7af3ea2554eba60c34c5 to your computer and use it in GitHub Desktop.
Save tureki/7af3ea2554eba60c34c5 to your computer and use it in GitHub Desktop.
json_encode null to empty string.
$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);
$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);
@maxcreed
Copy link

Thanks ! <3

@sleepyhead-km
Copy link

Thank you, this was driving me crazy!

@rasoul707
Copy link

thankks a lot

@ibobgunardi
Copy link

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