0

I'm having some trouble manipulating a complex array which looks like this:

Array
(
    [0] => Array
        (
            [checklist_position] => 8
            [checklist_id] => 2
            [question_id] => 11
            [section] => 1
            [sum_a] => 332611
            [sum_b] => 566201
        )

    [1] => Array
        (
            [checklist_position] => 9
            [checklist_id] => 2
            [question_id] => 12
            [section] => 1
            [sum_a] => 567725
            [sum_b] => 67301
        )

    [2] => Array
        (
            [checklist_position] => 10
            [checklist_id] => 2
            [question_id] => 13
            [section] => 1
            [sum_a] => 20004
            [sum_b] => 38381
        )

    [3] => Array
        (
            [checklist_position] => 11
            [checklist_id] => 2
            [question_id] => 14
            [section] => 2
            [sum_a] => 699144
            [sum_b] => 139456
        )

    [4] => Array
        (
            [checklist_position] => 12
            [checklist_id] => 2
            [question_id] => 15
            [section] => 2
            [sum_a] => 791204
            [sum_b] => 336133
        )

    [5] => Array
        (
            [checklist_position] => 13
            [checklist_id] => 2
            [question_id] => 16
            [section] => 2
            [sum_a] => 447501
            [sum_b] => 503112
        )

    [6] => Array
        (
            [checklist_position] => 14
            [checklist_id] => 2
            [question_id] => 17
            [section] => 2
            [sum_a] => 651332
            [sum_b] => 803628
        )

)

What I'm trying to do is to convert it to an array like this:

Array
(
    [0] => Array
        (
            [section] => 1
            [questions] => Array
                (
                    [0] => Array
                        (
                            [checklist_position] => 1
                            [checklist_id] => 1
                            [question_id] => 1
                            [section] => 1
                            [sum_a] => 348659
                            [sum_b] => 273072
                        )

                    [1] => Array
                        (
                            [checklist_position] => 2
                            [checklist_id] => 2
                            [question_id] => 2
                            [section] => 1
                            [sum_a] => 825992
                            [sum_b] => 189190
                        )

                )

        )

    [1] => Array
        (
            [section] => 2
            [questions] => Array
                (
                    [0] => Array
                        (
                            [checklist_position] => 1
                            [checklist_id] => 1
                            [question_id] => 1
                            [section] => 1
                            [sum_a] => 348659
                            [sum_b] => 273072
                        )

                    [1] => Array
                        (
                            [checklist_position] => 2
                            [checklist_id] => 2
                            [question_id] => 2
                            [section] => 1
                            [sum_a] => 825992
                            [sum_b] => 189190
                        )

                )

        )

)

The problem is I don't know where to begin and any tip is very appreciated.

Thank you!

3
  • Start with looping through your original array, and creating a new array. Commented Mar 26, 2012 at 16:29
  • So what is the pattern? slice array pieces (2 array each) and make new array with different structure ? Commented Mar 26, 2012 at 16:30
  • @safarov, yes, I want to build a new array with the structure I posted above. Commented Mar 26, 2012 at 16:32

2 Answers 2

1

@Patroklo's solution simplified:

$result = array();
foreach($array1 as $data){
    $section = $data['section'];
    $result[$section]['section'] = $section;
    $result[$section]['questions'][] = $data;
}

$result = array_values($result); // to re order

$array1 is your array with the current data and $result is what you expect

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you mrsafraz. The output is as expected. Got one more question though. Can $result start at 0? Now it starts at 1.
$result = array_values($result); - Check the answer again - I edited it. Regards.
1

Probably you'll have less trouble using the section number in the keys of the arrays, like (being $array1 the first array of your question):

foreach($array1 as $data)
{
    if(!array_key_exists($result[data['section']))
    {
        $result[$data['section']] = array('section' => $data['section']);
    }

    $result[$data['section']['questions'][] = $data;
}

Haven't checked it out, but it should make it.

3 Comments

I think you're missing some brackets over there. It's true that I get some output, but its not as desired.
Oh, so you mean somethin like $array1 = $original_array[0]?
No, $array1 = $original_array; You only have to loop through it, the only difference is that in the final array, instead of having the keys like -> 0, 1, 2, 3... they will be like: 1, 2

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.