将多维数组转换为另一个多维数组但具有不同的结构

时间:2012-03-26 16:22:29

标签: php multidimensional-array

我在操作一个看起来像这样的复杂数组时遇到了一些麻烦:

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
        )

)

我要做的是将它转换为这样的数组:

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
                        )

                )

        )

)

问题是我不知道从哪里开始,任何提示都非常感激。

谢谢!

2 个答案:

答案 0 :(得分:1)

使用数组键中的节号可能会少一些麻烦,比如($ array1是你问题的第一个数组):

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

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

没有检查出来,但它应该成功。

答案 1 :(得分:1)

@ Patroklo的解决方案简化:

$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是包含当前数据的数组,$ result是您期望的结果