【问题标题】:PHP: json with null values showing as empty when using json_decodePHP:使用 json_decode 时,具有空值的 json 显示为空
【发布时间】:2014-04-04 05:03:44
【问题描述】:

我有包含空值的 json。我仍然想捕获那些空值,但是当我使用 json_decode 时,空值在数组中被更改为空。有没有办法在使用 json_decode 后保留空值或将空值更改为空以外的值?

[2] => Array (
    [id] => 51136
    [key] => 18
    [fields] => Array (
        [ticket] =>
)  

在此数组中,我想将“票证”更改为空白以外的其他内容,或者在使用 json_decode 时保留原始空值也可以。

原始json数据:

"id": "51136",
"key": "18",
"fields": {
    "ticket": null,
          }

谢谢!

【问题讨论】:

  • null 会保留,你能显示你的原始数据吗?
  • 添加了原始json的sn-p

标签: php arrays json


【解决方案1】:

将空值更改为空以外的值?

您可以为此使用array_walk_recursive..

array_walk_recursive($yourarray, function (& $item, $key) {if (is_null($item)) { $item = "N/A"; }});

回调函数会检查您的数组中的 NULL 值,如果找到,则替换为 N/A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多