将ROWS分组为具有相同标识符的数组

时间:2013-05-10 05:41:56

标签: php

如何在foreach循环内部运行此数组,并将共享相同section_id的行组合在一起?

我已用注释块标记了所有行和部分。

这是数组

Array ( 
    [0] => Array ( // Row 1
        [assessment_selection_id] => 63 
        [assessment_id] => 32 
        [section_id] => 1 // Section 1
        [question_id] => 1 
        [selection] => 2 
        [timestamp] => 1368160586 
   ) 
   [1] => Array ( // Row 2
        [assessment_selection_id] => 61 
        [assessment_id] => 32 
        [section_id] => 2 // Section 2
        [question_id] => 1 
        [selection] => 3 
        [timestamp] => 1368160510 
   )
   [2] => Array ( // Row 3
        [assessment_selection_id] => 61 
        [assessment_id] => 32 
        [section_id] => 2 // Section 2
        [question_id] => 1 
        [selection] => 3 
        [timestamp] => 1368160510 
   ) 

   [3] => Array ( // Row 4
        [assessment_selection_id] => 61 
        [assessment_id] => 32 
        [section_id] => 2 // Section 2
        [question_id] => 1 
        [selection] => 3 
        [timestamp] => 1368160510 
   ) 
)


预期结果

Array ( 
   [0] => Array ( // Section 1
        [0] => Array ( // Row 1 
            [assessment_selection_id] => 63 
            [assessment_id] => 32 
            [section_id] => 1 
            [question_id] => 1 
            [selection] => 2 
            [timestamp] => 1368160586 
        )
    )
    [1] => Array ( // Section 2
        [0] => Array ( // Row 1
            [assessment_selection_id] => 61 
            [assessment_id] => 32 
            [section_id] => 2 
            [question_id] => 1 
            [selection] => 2 
            [timestamp] => 1368160586 
        )
       [1] => Array ( // Row 2
            [assessment_selection_id] => 61 
            [assessment_id] => 32 
            [section_id] => 2 
            [question_id] => 1 
            [selection] => 2 
            [timestamp] => 1368160586 
        )
       [2] => Array ( // Row 3
            [assessment_selection_id] => 61 
            [assessment_id] => 32 
            [section_id] => 2 
            [question_id] => 1 
            [selection] => 2 
            [timestamp] => 1368160586 
        )
    )
)


没有数组的预期结果

第1节

  • 第1行

    assessment_selection_id, assessment_id, SECTION_ID, question_id, 选择, 时间戳

第2节

  • 第1行

    assessment_selection_id, assessment_id, SECTION_ID, question_id, 选择, 时间戳

  • 第2行

    assessment_selection_id, assessment_id, SECTION_ID, question_id, 选择, 时间戳

  • 第3行

    assessment_selection_id, assessment_id, SECTION_ID, question_id, 选择, 时间戳

2 个答案:

答案 0 :(得分:5)

让我们认为你的数组保存在$ myArray中 这样做:

$newArray=array();
foreach($myArray as $val){
    $newKey=$val['section_id'];
    $newArray[$newKey][]=$val
}
print_r($newArray);

答案 1 :(得分:0)

@ SaVaFa逻辑几乎没有变化(根据问题作者所需的输出) 具有String值的数组键(关联数组)

$row = "section";
foreach($arrMain as $key){
    $sectionID=$key['section_id'];
    $arrResult[$row.$sectionID][]=$key;
}
echo "<pre>";print_r($arrResult);die;

输出

Array
(
    [section1] => Array
        (
            [0] => Array
                (
                    [assessment_selection_id] => 63
                    [section_id] => 1
                )

        )

    [section2] => Array
        (
            [0] => Array
                (
                    [assessment_selection_id] => 61
                    [section_id] => 2
                )

            [1] => Array
                (
                    [assessment_selection_id] => 61
                    [section_id] => 2
                )

            [2] => Array
                (
                    [assessment_selection_id] => 61
                    [section_id] => 2
                )

        )

)