我的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. No.1与Q. No. 8进行比较,那么当Q.8的时间到来时,它再次与Q.1进行比较。我想避免这种情况。我希望如果将Q.1与Q.8进行比较,那么当Q.8的转弯到来时,它不应该与Q. No.1进行比较。 有人可以帮我解决这方面的问题吗?任何形式的帮助都会受到高度赞赏。
答案 0 :(得分:2)
也许您可以更新第二个foreach以获取索引: foreach($ questions为$ inner_data){ 变成: foreach($ questions为$ secondIndex => $ inner_data){
并在其下添加: if($ secondIndex< = $ index) 继续;