【发布时间】:2013-12-09 22:31:38
【问题描述】:
我是 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
}
}
}
在上面的代码中,我在遇到问题的地方写了一条评论。
实际上,当条件(即$percentage > 50)得到满足时,我想将所有问题ID附加到原始数组$questions。假设问题 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 数组的新手。任何形式的建议都将受到欢迎。有人可以在这方面帮助我吗?任何帮助将不胜感激。
【问题讨论】:
-
一个好问题,因为您发布了所有需要的信息... :-) 我在下面的答案尝试
标签: php arrays multidimensional-array associative-array key-value