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

How can I run this array below inside of a foreach loop and group the rows together that share the same section_id?

I've labeled all the rows and sections with comment blocks.

This is the array

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


Expected result

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


Expected result without array

Section 1

  • Row 1

    assessment_selection_id, assessment_id, section_id, question_id, selection, timestamp

Section 2

  • Row 1

    assessment_selection_id, assessment_id, section_id, question_id, selection, timestamp

  • Row 2

    assessment_selection_id, assessment_id, section_id, question_id, selection, timestamp

  • Row 3

    assessment_selection_id, assessment_id, section_id, question_id, selection, timestamp

Let's think you have your array saved in $myArray Do this:

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

with little change in @SaVaFa's logic (as per Question author's desired output) Array keys with String value (associative array)

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

Output

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
                )

        )

)