如何通过键对多维数组求和和分组? [英] How to SUM and GROUP BY a multidimensional ARRAY by a key?

查看:83
本文介绍了如何通过键对多维数组求和和分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,正在尝试按一个键的值进行分组.

I have a multidimensional array and am trying to group by a value of one the keys.

所以

 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. 原始数组中的每个名称条目都具有相同的desc_topic子数组(例如,每个实例都具有相同的Apple/Banana/Orange值.
  2. qtd_posts子数组在相同的相应插槽中具有待分组的值(例如,将所有"1"项求和,将所有"2"项求和,等等...)
  3. 您要保留父级数组键,以便所有"Edward Foo"条目都将使用Edward Foo条目使用的第一个键(例如0)
  1. every name entry in the original array has an identical desc_topic sub-array (e.g. they all have the same Apple/Banana/Orange values for every instance.
  2. the qtd_posts sub-array has the to-be-grouped values in the same corresponding slots (e.g. all '1' entries are to be summed together, all '2' entries summed together, etc...)
  3. You want to preserve the parent array keys so that all 'Edward Foo' entries will use the first key used by an Edward Foo entry (e.g. 0)

如果适用,那么应该可以进行以下操作:

If that applies, then something like this should work:

$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;
    }
}

这篇关于如何通过键对多维数组求和和分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆