如何通过键对多维ARRAY进行SUM和GROUP BY?

时间:2010-05-13 14:17:51

标签: php arrays multidimensional-array grouping

我有一个多维数组,我试图用一个值组合键。

所以

 Array (
[0] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 10
                         [1] => 20
                         [2] => 50
                        )

    )


[1] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )  
        [qtd_posts] => Array  (
                         [0] => 10
                         [1] => 10
                         [2] => 10
                        )
    )

[2] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 5
                         [1] => 10
                         [2] => 30
                        )

    )

[3] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )             
        [qtd_posts] => Array  (
                         [0] => 8
                         [1] => 8
                         [2] => 20
                        )

    )            

我真的需要:

Array (
[0] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 15
                         [1] => 30
                         [2] => 80
                        )

    )

[1] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )  
        [qtd_posts] => Array  (
                         [0] => 18
                         [1] => 18
                         [2] => 30
                        )

           )

  )

1 个答案:

答案 0 :(得分:1)

我假设以下内容:

  1. 原始数组中的每个名称条目都有一个相同的desc_topic子数组(例如,它们对于每个实例都具有相同的Apple / Banana / Orange值。
  2. qtd_posts子阵列在相同的对应槽中具有待分组的值(例如,所有'1'条目将被加在一起,所有'2'条目被加在一起等等... )
  3. 您希望保留父数组键,以便所有'Edward Foo'条目将使用Edward Foo条目使用的第一个键(例如0)
  4. 如果适用,那么这样的事情应该有效:

    $newarr = array();
    $reverse_map = array();
    
    foreach($array as $idx => $entry) {
        if (isset($reverse_map[$entry['name']]) {
             // have we seen this name before? retrieve its original index value
             $idx = $reverse_map[$entry['name']]; 
        } else {
             // nope, new name, so store its index value
             $reverse_map[$entry['name']] = $idx;
        }
    
        // copy the 'constant' values
        $newarr[$idx]['name'] = $entry['name'];
        $newarr[$idx]['desc_top'] = $entry['desc_topic'];
    
        // sum the qtd_post values to whatever we previously stored.        
        foreach($entry['qtd_posts'] as $x => $y) {
            $newarr[$idx]['qtd_posts'][$x] += $y;
        }
    }