Php 从数组中删除空值

Php 从数组中删除空值,php,mysql,Php,Mysql,尝试使用数据库中的值从数组中删除空值时遇到问题。这些空值通常在“答案”中找到。。代码如下: $getQuestions = mysql_logging_query("SELECT fq.`question_id`, fq.`ques_form_id`, fq.`question_body`, fq.`type`, fq.`answer1`, fq.`answer2`, fq.`answer3`, fq.`answer4`, fq.`answer5`, fq.`answer6`, fq.`

尝试使用数据库中的值从数组中删除空值时遇到问题。这些空值通常在“答案”中找到。。代码如下:

    $getQuestions = mysql_logging_query("SELECT fq.`question_id`, fq.`ques_form_id`, fq.`question_body`, fq.`type`, fq.`answer1`, fq.`answer2`, fq.`answer3`, fq.`answer4`, fq.`answer5`, fq.`answer6`, fq.`min_validation`, fq.`max_validation`
                                                FROM QuestionnaireFormQuestions fq
                                                LEFT JOIN QuestionnaireForms f
                                                ON f.`form_id` = fq.`ques_form_id`
                                                WHERE f.`active` = 1
                                                AND f.`form_id` = '".mysql_real_escape_string($form_id,$this->dbcon)."'
                                                ",$this->dbcon);

            if($getQuestions && mysql_num_rows($getQuestions)>0 && mysql_error($this->dbcon)=="")
            {           
                $get_questions_array = array();

                while($getQuestions && $getQuestions_rec=mysql_fetch_assoc($getQuestions))
                {   
                    $get_questions_array[] = array('question_id' => $getQuestions_rec['question_id'], 
                                                'related_form_id' => $getQuestions_rec['ques_form_id'],
                                                'question_body' => $getQuestions_rec['question_body'],
                                                'question_type' => $getQuestions_rec['type'],
                                                'possible_answer1' => $getQuestions_rec['answer1'],
                                                'possible_answer2' => $getQuestions_rec['answer2'],
                                                'possible_answer3' => $getQuestions_rec['answer3'],
                                                'possible_answer4' => $getQuestions_rec['answer4'],
                                                'possible_answer5' => $getQuestions_rec['answer5'],
                                                'possible_answer6' => $getQuestions_rec['answer6'],
                                                'min_validation' => $getQuestions_rec['min_validation'],
                                                'max_validation' => $getQuestions_rec['max_validation']
                                              );
                }
                if(is_array($get_questions_array) && count($get_questions_array)>0)
                {
                    foreach($get_questions_array as $key=>$array)
                    {
                        if($array === null) {
                            unset($get_questions_array[$key]);
                        }
                        $return['data']['questions'][] = $array;
            }
        }
}
else
 //error
例如,报税表;看起来是这样的:

"question_id":"3",
"related_form_id":"4",
"question_body":"Do you like this content?",
"question_type":"radio",
"possible_answer1":"Disagree",
"possible_answer2":"Neutral",
"possible_answer3":"Agree",
"possible_answer4":null,
"possible_answer5":null,
"possible_answer6":null,
"min_validation":"1",
"max_validation":"1"

我尝试过使用empty和isnull取消设置密钥,但没有效果。任何帮助都将不胜感激。

您为什么不查询不包含空值的数据,而不是自己删除空值?让数据库为您执行以下操作:

select * from table where possible_answer IS NOT NULL

为什么不查询不包含空值的数据,而不是自己删除空值?让数据库为您执行以下操作:

select * from table where possible_answer IS NOT NULL

在数组中尝试此循环,如果值为空,则使用键的将其设置为null

foreach($getQuestions_rec as $key => $value){
     if($value == ''){
        $getQuestions_rec[$key] = null;
     }
}

在数组中尝试此循环,如果值为空,则使用键的将其设置为null

foreach($getQuestions_rec as $key => $value){
     if($value == ''){
        $getQuestions_rec[$key] = null;
     }
}
使用IFNULL(ColumnName,“”)替换SQL查询中空字符串的null值

示例:IFNULL(answer6,””

使用IFNULL(ColumnName,”)替换SQL查询中空字符串的null值


示例:如果为null(answer6,”)请参考此项,或者您可以更改查询以从表中选择null值。

请参考此项,或者您可以更改查询以从表中选择null值。

我认为您可能在错误的级别上循环使用嵌套数据结构

您有一个
$get\u questions\u数组
,其中的每个元素都是一个关联数组,来自MySQL结果集中的一行。但是php代码在结果集的行上循环,而不是在列上循环

我想您需要更像这样的东西,还有另一个嵌套的
foreach

if(is_array($get_questions_array) && count($get_questions_array)>0)
{
    foreach($get_questions_array as $row)
    {
        foreach ($row AS $colname=>$colvalue)
        {
            if ($colvalue === null || $colvalue =='')
            {
                unset($row[$colname]);
            }
        }
    }
}

看看发生了什么?您的代码丢弃了所有空行,但没有任何空行,因此它什么也没做。

我认为您可能在错误的级别上循环使用嵌套数据结构

您有一个
$get\u questions\u数组
,其中的每个元素都是一个关联数组,来自MySQL结果集中的一行。但是php代码在结果集的行上循环,而不是在列上循环

我想您需要更像这样的东西,还有另一个嵌套的
foreach

if(is_array($get_questions_array) && count($get_questions_array)>0)
{
    foreach($get_questions_array as $row)
    {
        foreach ($row AS $colname=>$colvalue)
        {
            if ($colvalue === null || $colvalue =='')
            {
                unset($row[$colname]);
            }
        }
    }
}

看看发生了什么?您的代码丢弃了整个空行,但没有任何空行,因此它没有做任何事情。

您没有测试数组中的值,您需要:

foreach($get_questions_array as $array){
    foreach($array as $key=>$element){
          if($element===null)
               unset($array[$key]);
          }
          $return['data']['questions'][] = $array;
}

您不是在测试数组内的值,您需要:

foreach($get_questions_array as $array){
    foreach($array as $key=>$element){
          if($element===null)
               unset($array[$key]);
          }
          $return['data']['questions'][] = $array;
}

如果要在查询后执行此操作,请使用
array\u filter()
。是否尝试从返回的问题中删除空字段(即通常可能的答案)?您似乎在循环问题并检查整个返回数组是否为空,而不是循环每个问题的字段并检查每个字段是否为空。如果要在查询后执行此操作,请使用
array\u filter()从一个返回的问题?您似乎在循环问题并检查整个返回数组是否为null,而不是循环每个问题的字段并检查每个字段是否为null。这非常有效,谢谢。我现在明白问题了。这很好用,谢谢。我现在明白问题所在了。