1

I am trying to count the values of an associative array, but it isnt doing what I want it to.

I am pulling data from a database, as I need all the answers to a question. The answers in the database are under the fields answer1, answer2, answer3 etc, up to answer20.

What I am trying to do is count the number of fields that contain data, and ignore the fields that dont have any data.

Here is my code so far:

<?php
        $results = $db->get_results("SELECT * FROM wellness_hra_questions WHERE category='general' AND level='1' AND gender='All' AND active='yes' ORDER BY id ASC");
        foreach($results as $result){
            $id = $result->id;
        } 

                    $answers = $db->get_results("SELECT answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, answer11, answer12, answer13, answer14, answer15, answer16, answer17 FROM wellness_hra_questions WHERE id=".$id."");
                        //$db->debug();


                        for( $i=1; $i <= 17; $i++) {

                            foreach($answers as $a_result){
                                $answer = 'answer';
                                $answer_id = $answer.$i;
                                $answer_id2 = $a_result->$answer_id;

                                $answer1 = $a_result->answer1;
                                $array1 = array('1'=>''.$answer1.'');

                                $answer2 = $a_result->answer2;
                                $array2 = array('2'=>''.$answer2.'');

                                $answer3 = $a_result->answer3;
                                $array3 = array('3'=>''.$answer3.'');

                                $answer4 = $a_result->answer4;
                                $array4 = array('4'=>''.$answer4.'');

                                $answer5 = $a_result->answer1;
                                $array5 = array('5'=>''.$answer5.'');

                                $answer6 = $a_result->answer6;
                                $array6 = array('1'=>''.$answer6.'');
                            }

                                if($answer1 == TRUE){
                                    $newvalue1 = $array1;   
                                }
                                if($answer2 == TRUE){
                                    $newvalue2 = $array2;   
                                }
                                if($answer3 == TRUE){
                                    $newvalue3 = $array3;   
                                }
                                if($answer4 == TRUE){
                                    $newvalue4 = $array4;   
                                }
                                if($answer5 == TRUE){
                                    $newvalue1 = $array5;   
                                }
                                if($answer1 == TRUE){
                                    $newvalue6 = $array6;   
                                }

                                $newarray = array_merge($newvalue1,$newvalue2, $newvalue3, $newvalue4, $newvalue5);

                                $count = count($newarray, COUNT_RECURSIVE);
                                echo $count;
                        }
?>          
2
  • 1
    It looks like this application could improve on the database structure by moving the answerX columns into its own table. Commented Sep 23, 2016 at 15:46
  • Assuming the rest of your logic is correct then this should work: $count = array_sum(array_map(function($v){return ($v!=='' && $v!==null ? 1 : 0);}, $new_array)); Commented Sep 23, 2016 at 15:52

1 Answer 1

1

You need a code cleanup here - if what you want is a count, and the array_merge is ONLY for this purpose, this should do what I believe you would like to do:

for( $i=1; $i <= 17; $i++) {
    foreach($answers as $a_result){

        $answer_id = 'answer'.$i;
        $answer_id2 = $a_result->$answer_id;

        $thisCount = 0;
        $newarray = array();
        for($j = 1; $j < 7; $j++) {
            $var = "answer" . $j;
            $$var = ${"a_result->answer" . $j};

            if (strlen($$var) > 0) {
                $arrayID = "array" . $j;
                $$arrayID = array((string)$j => $$var);// I don't think you need this but if you do included array_merge here
                array_merge($newarray, $$arrayID);
                $thisCount++;
            }
        }

        echo $count;
        print_r($newarray); // show the contents of the merged arrays
    }// end foreach
}//end for
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.