【问题标题】:How to create an inner array inside parent array in following scenario?在以下场景中,如何在父数组中创建内部数组?
【发布时间】: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


【解决方案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 循环必须引用数组元素(使用 &),因为它们需要修改数组...

【讨论】:

  • :感谢您的回答和宝贵建议。请您帮我实现您向我提出的想法好吗?
  • 你必须从那里拿走它或找一个有更多空闲时间的人 - 我在工作:'(
  • :没问题,只要有时间,请尽量给我最佳代码。在那之前,我会代表我尝试。再次感谢您的帮助。
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 2015-10-21
  • 2019-08-18
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
相关资源
最近更新 更多