2014-11-05 100 views
0

我无法尝试使用数据库中的值从数组中删除空值。这些空值通常在'答案'中找到。代码如下:删除数组中的空值

$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" 

我试过unsetting使用空和isnull的密钥,但无济于事。任何帮助,将不胜感激。

+0

如果你想查询后做,然后使用'array_filter()'。 – 2014-11-05 13:07:23

+0

你是否试图从返回的问题中删除空字段(即通常可能的答案)?您好像循环遍历问题并检查整个返回的数组为null,而不是遍历每个问题的字段并检查每个字段的空值。 – Kickstart 2014-11-05 13:21:46

回答

2

你是不是测试数组中的值,您需要:

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

这工作完美,谢谢。我现在看到了这个问题。 – Veltu 2014-11-05 13:23:50

0

为什么不查询不包含空值的数据,而不是自己去除空值?让你像数据库这样做:

select * from table where possible_answer IS NOT NULL 
0

尝试通过阵列的这个循环,并将它们与关键的为空,如果为空值

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

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

实施例:IFNULL(answer6, “”)

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]); 
      } 
     } 
    } 
} 

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