在下面的场景中,如何在父数组中创建内部数组


How to create an inner array inside parent array in following scenario?

我是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数组的新手。任何形式的建议都将受到欢迎。有人能在这方面帮我吗?如有任何帮助,我们将不胜感激。

在外部循环中,初始化数组

$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循环必须引用数组元素(使用&),因为它们需要修改数组。。。