2014-11-05 103 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]); 
      } 
     } 
    } 
} 

看看到底發生了什麼?你的代碼丟掉了整行爲空的行,但沒有任何行,所以它沒有做任何事情。