0

I'm having trouble trying to remove null values from an array using values from the database. These null values are usually found within the 'answers'.. Code below:

    $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

A return for example; would look like this:

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

I've tried unsetting the key using empty and isnull but to no avail. Any help would be appreciated.

Veltu
  • 53
  • 10
  • If you want to do it after the query, then use `array_filter()`. – Amal Murali Nov 05 '14 at 13:07
  • Are you trying to remove NULL fields (ie, normally possible answers) from a returned question? You appear to be looping through the questions and checking the whole returned array for null, rather than looping around the fields for each question and checking each field for null. – Kickstart Nov 05 '14 at 13:21

6 Answers6

2

I think it's possible you're looping through your nested data structure at the wrong level.

You have this $get_questions_array, each element of which is an associative array that came from one row in your MySQL result set. But your php code loops over the rows of the result set, not over the columns.

I think you want something more like this, with another nested 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]);
            }
        }
    }
}

See what's going on? Your code was throwing away whole rows that were null, but there weren't any of those, so it wasn't doing anything.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
2

You are not testing the values inside the array, you need to:

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

why don't you query the data that does not contain nulls, instead of removing the nulls by yourself ? let the database do this for you something like:

select * from table where possible_answer IS NOT NULL
Unix von Bash
  • 735
  • 5
  • 20
0

Try this loop through the array and set them to null with the key's if the value is empty

foreach($getQuestions_rec as $key => $value){
     if($value == ''){
        $getQuestions_rec[$key] = null;
     }
}
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
0

Use the IFNULL(ColumnName,"") to replace the null values to empty string in SQL query.

Example: IFNULL(answer6,"")

Saravana Kumar
  • 3,669
  • 5
  • 15
  • 35
0

refer this this How to remove null values from an array? or you ca alter the query to select the null values from table.

Community
  • 1
  • 1
Sarath
  • 2,318
  • 1
  • 12
  • 24