Php 如何避免在以下场景中重新比较字符串?

Php 如何避免在以下场景中重新比较字符串?,php,arrays,loops,foreach,associative-array,Php,Arrays,Loops,Foreach,Associative Array,我的PHP代码片段如下所示: $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 = $questi

我的PHP代码片段如下所示:

$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('which','who','what','how','when','whom','wherever','the');
/*This loop removes all the words of $exclude_words array from all questions*/
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 );
}

/*Now the actual comparison of each question with every other question stats here*/
foreach ($questions as $index=>$outer_data) {
    $questions_data[$index]['similar_questions_ids_and_percentage'] = Array();        
    $outer_question = $outer_data['question_text'];
    $qpcnt = 0;     
    foreach ($questions as $inner_data) {
        /*This is to avoid comparing the question with itself*/
        if ($outer_data['question_id'] != $inner_data['question_id']) {
            $inner_question = $inner_data['question_text'];  
            /*This is to calculate percentage of match between each question with every other question*/
            /*In this loop I want single time comparison of each question with every other question Now it's getting repeated, please help me here*/
            $same_chars = similar_text($outer_question, $inner_question, $percent);
            $percentage = number_format((float)$percent, 2, '.', '');

            /*If $percentage is >= $percent_match only then push the respective question_id into an array*/
            if($percentage >= 50) {
                $questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['question_id'] = $inner_data['question_id'];
                $questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['percentage']  = $percentage;
            $qpcnt++;
            }
        }   
    }      
}
实际上,我希望避免在上述代码的内部foreach循环中进行重新比较。 例如,假设有十个问题,每个问题都与其他所有剩余的问题进行比较。如果把第一个问题和第八个问题进行比较,那么到了第八个问题的时候,它又和第一个问题进行了比较。我想避免这种情况。我想,如果把Q.1和Q.8进行比较,那么当第8个问题到来时,它不应该和Q.1进行比较。
在这方面有人能帮我吗?非常感谢您提供的任何帮助。

也许您可以更新第二个foreach以获得索引: foreach($内部数据形式的问题){ 改为: foreach($secondIndex=>$Internal_数据形式的问题){

在下面加上: 如果($secondIndex