2

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, selection, タイムスタンプ

第2節

  • 行 1

    assessment_selection_id, assessment_id, section_id, question_id, selection, タイムスタンプ

  • 行 2

    assessment_selection_id, assessment_id, section_id, question_id, selection, タイムスタンプ

  • 行 3

    assessment_selection_id, assessment_id, section_id, question_id, selection, タイムスタンプ

4

2 に答える 2

5

配列が $myArray に保存されているとしましょう。

$newArray=array();
foreach($myArray as $val){
    $newKey=$val['section_id'];
    $newArray[$newKey][]=$val
}
print_r($newArray);
于 2013-05-10T05:59:15.097 に答える
0

@SaVaFa のロジックをほとんど変更せずに (質問の作成者が希望する出力に従って) 文字列値を持つ配列キー (連想配列)

$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
                )

        )

)
于 2013-05-10T06:16:12.793 に答える