Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Codeigniter中将json空值自动转换为空字符串_Php_Json_Codeigniter_Codeigniter 2 - Fatal编程技术网

Php 如何在Codeigniter中将json空值自动转换为空字符串

Php 如何在Codeigniter中将json空值自动转换为空字符串,php,json,codeigniter,codeigniter-2,Php,Json,Codeigniter,Codeigniter 2,我在Codeigniter中使用json,我有一些字段输出空值,我想将这些空值更改为空字符串。我不想逐个检查每个字段。我的CI代码如下: $result= $this->get_model->get_model(); header("content-type: application/json"); $data[] = array('name'=>"result",'data'=>$result); echo json_encode(array('success'=>

我在Codeigniter中使用json,我有一些字段输出空值,我想将这些空值更改为空字符串。我不想逐个检查每个字段。我的CI代码如下:

$result= $this->get_model->get_model();
header("content-type: application/json");
$data[] = array('name'=>"result",'data'=>$result);
echo json_encode(array('success'=> 1,'posts'=>$data));
return $result; 

试试这段代码。它打印json,用空值替换空值

$result= $this->get_model->get_model();
header("content-type: application/json");
$data[] = array('name'=>"result",'data'=>$result);
$data_result = json_encode(array('success'=> 1,'posts'=>$data));

echo str_replace("null","",$data_result);

return $result; 

这是最简单的方法:

array_walk_recursive($array,function(&$item){$item=strval($item);});

From:

转换为字符串后。用空字符串重放
null
,你能用例子解释一下吗,或者给我一个链接吗?你能告诉我什么输出
echo json_编码(数组('success'=>1,'posts'=>$data))这行打印。这是代码片段:“名称”:“最新”,“数据”:[{“时间戳”:“2015-11-16 11:34:03”,“平均评级”:null}]我尝试了上述代码,但出现了相同的空值。我的代码
echo
打印的内容,请让我知道,
$data\u result
print\r($data\u result)的值是多少;它不输出任何内容这意味着$data\u result中没有值。
$data\u result
是一个字符串。它不是数组。因此,您应该使用
echo
@sandepsue-我在json中遇到错误,我已将图像上传到获取它的位置。所有平均值都会像上图一样发生变化。