Php 如何防止json中出现空值?

Php 如何防止json中出现空值?,php,json,Php,Json,Am将动态值存储在数组中,并使用json_encode转换json,转换后Am打印json值,但有些值返回空值,但Am打印数组值,它显示正确,我能做什么,请帮助我 我的代码如下: header('Content-type: application/json'); $return=array(); $level=$_REQUEST['level']; $sql="SELECT * FROM guess WHERE level=$level"; $res = $st->query($sql);

Am将动态值存储在数组中,并使用json_encode转换json,转换后Am打印json值,但有些值返回空值,但Am打印数组值,它显示正确,我能做什么,请帮助我

我的代码如下:

header('Content-type: application/json');
$return=array();
$level=$_REQUEST['level'];
$sql="SELECT * FROM guess WHERE level=$level";
$res = $st->query($sql);
if($res->num_rows>=1){
    $images=array();
    while($row= $res->fetch_assoc())
    {
        $images[]=$row;
    }
    $return['list']=$images;
}
echo json_encode($return);
我的json输出

{"list":[{"id":"1","type":"text","question":null,"answer":"feed"}]
列队

[list] => Array
    (
        [0] => Array
            (
                [id] => 1
                [type] => text
                [question] => WHAT CHARITY Christian organization committed to feeding God’s children hungry in body and spirit?
                [answer] => feed
            )


    )

json_encode期望数据中的字符串编码为UTF-8

   header('Content-type: application/json');
    $return=array();
    $level=$_REQUEST['level'];
    $sql="SELECT * FROM guess WHERE level=$level";
    $res = $st->query($sql);
    if($res->num_rows>=1){
        $images=array();
        while($row= $res->fetch_assoc())
        {
            $images[]['id']= ($row['id']);
            $images[]['type']= ($row['type']);
            $images[]['question']= utf8_encode($row['question']);//Convert this to UTF-8 
            $images[]['answer']= ($row['answer']);
        }
        $return['list']= $images;
    }
echo json_encode($return);

json_encode期望数据中的字符串编码为UTF-8

   header('Content-type: application/json');
    $return=array();
    $level=$_REQUEST['level'];
    $sql="SELECT * FROM guess WHERE level=$level";
    $res = $st->query($sql);
    if($res->num_rows>=1){
        $images=array();
        while($row= $res->fetch_assoc())
        {
            $images[]['id']= ($row['id']);
            $images[]['type']= ($row['type']);
            $images[]['question']= utf8_encode($row['question']);//Convert this to UTF-8 
            $images[]['answer']= ($row['answer']);
        }
        $return['list']= $images;
    }
echo json_encode($return);

使用mysql
IFNULL
函数并修改您的sql查询,这样您就不必编写额外的php代码,因为如果第一个参数(问题列)值为null,IFNULL函数将返回第二个参数(您所需的字符串)值

SELECT col1, col2, IFNULL(question, 'WHAT CHARITY Christian organization committed to feeding God\’s children hungry in body and spirit?') as question  FROM guess WHERE level=$level

使用mysql
IFNULL
函数并修改您的sql查询-这样您就不必编写额外的php代码,因为如果第一个参数(问题列)值为null,IFNULL函数将返回第二个参数(您所需的字符串)值

SELECT col1, col2, IFNULL(question, 'WHAT CHARITY Christian organization committed to feeding God\’s children hungry in body and spirit?') as question  FROM guess WHERE level=$level

谢谢你的回答,但我想要json中的value而不是null值。我用代码编辑了我的答案,谢谢你的评论。@Silambarasan你想要什么值来代替null而不是null我想要“什么慈善基督教组织致力于喂养饥肠辘辘的上帝的孩子?”。此值在数组中正确显示。数组值的输出已在上述问题中给出。感谢您的回答,但我希望json中有值而不是空值。我已用代码编辑了我的答案,感谢您的评论。@Silambarasan您想要什么值来代替空值而不是空值我想要什么值“哪个基督教慈善组织致力于喂养身体和精神饥饿的上帝的孩子?”。此值在数组中正确显示。数组值的输出已在上述问题中给出。