Skip to content

Instantly share code, notes, and snippets.

@neatstudio
Created June 30, 2017 00:37
Show Gist options
  • Save neatstudio/441fab9f7d3100f6fcea3ad68f0214a0 to your computer and use it in GitHub Desktop.
Save neatstudio/441fab9f7d3100f6fcea3ad68f0214a0 to your computer and use it in GitHub Desktop.
PHP JSON convert null to string
<?php
$data = [
"a" => 1,
"b" => null,
"c" => [
"d" => 2,
"e" => null,
"f" => [
"g" => 3,
"h" => null
]
],
null
];
function safeJson($arr){
array_walk_recursive($arr, function (&$item, $key) {
$item = is_null($item) ? '' : $item;
});
return $arr;
}
echo json_encode(safeJson($data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment