Created
June 30, 2017 00:37
-
-
Save neatstudio/441fab9f7d3100f6fcea3ad68f0214a0 to your computer and use it in GitHub Desktop.
PHP JSON convert null to 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
<?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