【发布时间】:2013-12-11 13:52:40
【问题描述】:
我的PHP代码sn-p如下:
$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 进行比较。 任何人都可以在这方面帮助我吗?任何形式的帮助都将不胜感激。
【问题讨论】:
标签: php arrays loops foreach associative-array