Question

I'm a newbie to the complicated arrays in PHP. I'm having an associative array named $questions as follows(For your reference I'm printing only first five elements of this associative array, the actual array is too large):

Array
(
    [0] => Array
        (
            [question_id] => 24264
            [question_parent_id] => 0
            [question_subject_id] => 20
            [question_topic_id] => 544
            [question_directions] => 
            [question_text] => Which of the following is the consequence of plant diseases?
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 1
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_site_id] => 
            [question_created_staff_id] => e516459cde6a92869a887cb99a911cd6
            [question_added_date] => 1326866014
            [question_updated_staff_id] => 
            [question_updated_date] => 0
        )

    [1] => Array
        (
            [question_id] => 24269
            [question_parent_id] => 0
            [question_subject_id] => 20
            [question_topic_id] => 544
            [question_directions] => 
            [question_text] => Viruses enter into their host through
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 1
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_site_id] => 
            [question_created_staff_id] => e516459cde6a92869a887cb99a911cd6
            [question_added_date] => 1326866089
            [question_updated_staff_id] => 
            [question_updated_date] => 0
        )

    [2] => Array
        (
            [question_id] => 24274
            [question_parent_id] => 0
            [question_subject_id] => 20
            [question_topic_id] => 544
            [question_directions] => 
            [question_text] => which of the following category of plant diseases cannot be controlled by chemical treatment ?
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 1
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_site_id] => 
            [question_created_staff_id] => e516459cde6a92869a887cb99a911cd6
            [question_added_date] => 1326866169
            [question_updated_staff_id] => 
            [question_updated_date] => 0
        )

    [3] => Array
        (
            [question_id] => 24279
            [question_parent_id] => 0
            [question_subject_id] => 20
            [question_topic_id] => 544
            [question_directions] => 
            [question_text] => Plants can be made disease resistant through
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 1
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_site_id] => 
            [question_created_staff_id] => e516459cde6a92869a887cb99a911cd6
            [question_added_date] => 1326866226
            [question_updated_staff_id] => 
            [question_updated_date] => 0
        )

    [4] => Array
        (
            [question_id] => 24282
            [question_parent_id] => 0
            [question_subject_id] => 20
            [question_topic_id] => 544
            [question_directions] => 
            [question_text] => Potato famine of Ireland occured in
            [question_file] => 
            [question_description] => 
            [question_difficulty_type] => 1
            [question_has_sub_ques] => 0
            [question_picked_individually] => no
            [question_appeared_count] => 0
            [question_manual] => 0
            [question_site_id] => 
            [question_created_staff_id] => e516459cde6a92869a887cb99a911cd6
            [question_added_date] => 1326866259
            [question_updated_staff_id] => 
            [question_updated_date] => 0
        )
)

Now I want to compare each question's ['question_text'] key's value with with every other question's ['question_text'] key's value present in an array. I've written following code for it but it has few following drawback as follows:

  1. During the comparison the question itself is getting compared with itself which shouldn't be done.

My code is as follows:

function GetSimilarQuestionsBySubjectIdTopicId($subject_id, $topic_id) {

            $sql  = " SELECT * FROM ".TBL_QUESTIONS." WHERE question_subject_id=".$subject_id;
            $sql .= " AND question_topic_id=".$topic_id;

            $this->mDb->Query($sql);
            $questions_data = $this->mDb->FetchArray();
            $questions      = $questions_data;

            $exclude_words = array('the','at','is','are','when','whom');
foreach($questions as $index=>$arr) {
        $questions_array = explode(' ',$arr['question_text']);
        $clean_questions = array_diff($questions_array, $exclude_words);
        $questions[$index]['question_text'] = implode(' ',$clean_questions );
      }
foreach ($questions as $index=>$outer_data) {

        $outer_data['similar_questions_ids'] = Array();
        $outer_question = $outer_data['question_text'];

        foreach ($questions as $inner_data) {

          $inner_question = $inner_data['question_text'];  

            $same_chars = similar_text($outer_question, $inner_question, $percent);
            $percentage = number_format((float)$percent, 2, '.', '');

            if($percentage > 50) 
                $questions_data[$index]['similar_questions_ids'][] = $inner_data['question_id'];
        }   
     }
}

Can anyone please help resolve my issue? Also if you have any idea to optimize my existing code, it will be welcomed.

Was it helpful?

Solution 2

To avoid comparing a question to itself, you could try to first check if the question's ID is not the same as the one it is being compared with. So, just before you start the comparision add something like

if ($question['question_id'] !== $compared_question['question_id']) {
    // Your compare code
}

Can't really see in your code exactly where you should place it, but I hope you get the idea!

OTHER TIPS

Follow as @Galden suggested. It will just avoid the for/loop operation if the question_id is same. Talking about optimization, remove this line

 $questions = $questions_data;

It's not necessary and makes your script slow.

 $questions_data = $this->mDb->FetchArray();

Above line is just enough. Use this variable instead of assigning a variable to another variable for no good reason.

foreach($questions as $index1=>$outer_data) {
    foreach($questions as $index2=>$inner_data) {
        if($index1 != $index2){ //check the indexes(keys) 
        //your compare code
        }

//... Hope it helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top