从数组中删除空值

时间:2014-11-05 13:03:15

标签: 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.`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"

我尝试使用空和isnull取消设置密钥,但无济于事。任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:2)

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

你有这个$get_questions_array,其中每个元素都是一个关联数组,来自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]);
            }
        }
    }
}

看看发生了什么?你的代码丢弃了整行的空行,但没有任何行,所以它没有做任何事情。

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

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

select * from table where possible_answer IS NOT NULL

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

使用IFNULL(ColumnName,"")将空值替换为 SQL查询中的空字符串。

示例:IFNULL(answer6,"")

答案 5 :(得分:0)

<首选此How to remove null values from an array?,或者您可以更改查询以从表中选择空值。