2

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

4
  • the first [assessment_selection_id] with [timestamp] => 1368160586 should be 63, not 61 :) Commented May 10, 2013 at 5:50
  • Thanks, I think the main focus is on the section_id's being correct, but I'll address your issue. Commented May 10, 2013 at 5:51
  • Do you need to have the key for the sections as shown in your example or any other value for them could work? Commented May 10, 2013 at 5:53
  • I think ordering by a numerical key would be the best. But, it can always be changed. Commented May 10, 2013 at 5:55

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@SaVaFa would you give me an example that will echo the section name and rows out in php? Having some trouble for some reason.
0

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
                )

        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.