使用PHP在多维数组中合并相同的数组值

时间:2010-02-18 04:14:51

标签: php

我有这个多维数组:

Array
(
    [0] => Array
        (
            [name] => lorem
            [id] => 1
            [type] => Q1
            [q1] => 39.55
            [q2] => 0

        )

    [1] => Array
        (
            [name] => lorem
            [id] => 1
            [type] => Q2
            [q1] => 0;
            [q2] => 39.55
        )

    [2] => Array
        (
            [name] => lorem
            [id] => 1
            [type] => Q1
            [q1] => 39.55
            [q2] => 39.55
        )

    [3] => Array
        (
            [owner_name] => name
            [id] => 2
            [type] => Q1
            [q1] => 39.55
        [q2] => 0
        )

)

我希望将数组中所有项目与相同的id组合在一起,并将同一所有者中的所有q1和q2组合在同一类型中,从而创建新的数组结构:

Array
(
    [0] => Array
        (
            [name] => lorem
            [id] => 1
        [Q1_total = > 79.1
            [q2_total] => 79.1
        )

    [1] => Array
        (
            [owner_name] => name
            [id] => 2
            [type] => Q1
            [Q1_total] => 39.55
        [Q2_total] => 0;
        )

)

1 个答案:

答案 0 :(得分:0)

您必须使用 array_map 功能并创建自定义处理函数以根据需要处理数组。