我是PHP中复杂数组的新手。
我有一个名为$questions
的关联数组,如下所示(为了您的参考,我只打印这个关联数组的前两个元素,实际数组太大了):
Array
(
[0] => Array
(
[question_id] => 33185
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => Two gases are at 300 K and 350 K respectively Ratio of average kinetic energy of their molecules is
[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] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180210
[question_updated_staff_id] =>
[question_updated_date] => 0
)
[1] => Array
(
[question_id] => 33187
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => what will be the temperature when the rms velocity is double the rms velocity at 300 K
[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] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180274
[question_updated_staff_id] =>
[question_updated_date] => 0
)
)
现在我对这个数组的操作代码如下:
/*Compare each question with all the questions present within an array*/
foreach ($questions as $outer_data) {
$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) {
//I'm not able to write a perfect code to create an array here, please help me out in writing this code
}
}
}
在上面的代码中我写了一条评论,我面临着一个问题。
实际上,当条件(即$questions
)得到满足时,我想将所有问题ID附加到原始数组$percentage > 50
。假设对于问题id 33185,下面的id具有类似的问题,则具有question_id值为33185的数组元素应如下所示:
Array
(
[0] => Array
(
[question_id] => 33185
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => Two gases are at 300 K and 350 K respectively Ratio of average kinetic energy of their molecules is
[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] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180210
[question_updated_staff_id] =>
[question_updated_date] => 0
[similar_questions_ids] => Array
(
[0] => 60905
[1] => 60929
[2] => 60912
)
)
)
现在我面临着为这个理想输出编写完美代码的问题。如果我以错误的方式编写了当前的代码,你就可以把它想象一下,因为我是PHP中的数组的新手。任何形式的建议都会受到欢迎。请问有人可以在这方面帮助我吗?任何帮助将受到高度赞赏。
答案 0 :(得分:1)
在外部循环中,初始化数组
$outer_data['similar_questions_id'] = Array();
在内部循环中,如果超出百分比,则添加到数组...
$outer_data['similar_questions_id'][] = $inner_question['question_id'];
我不确定你为什么要在百分比上做出时髦的数字格式;我认为不需要它。如果浮动是这里的问题,则比较50.0。
想法:您可能不需要在内部循环中遍历整个数组。您可以通过注意到如果一个问题与另一个问题类似,反之亦然,并同时分配两个百分比,则可以提高性能。你的内部循环只需要从外部循环所需的索引进行扫描。
foreach ($questions as $i => &$outer_data) {
for ($j = $i+1; $j < length($questions); $j++) {
$inner_data = &$questions[$j];
....
}
}
我相信foreach循环必须引用数组元素(使用&amp;),因为它们需要修改数组...