PHP - 如何总结和GROUP BY通过一键多维数组? [英] PHP - How to SUM and GROUP BY a multidimensional ARRAY by a key?

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

问题描述

我有一个多维数组和我一个键的值试图组。

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 子阵列(例如,它们都对每一个实例相同的苹果/香蕉/橙子值。

  2. qtd_posts 子阵有要被划分在同一个相应的槽值(例如,所有1项将被累加在一起,所有的'2 项加在一起,等...)

  3. 您想preserve父数组键让所有的爱德华富'的条目将使用由爱德华·美孚条目(例如,0)
  4. 使用的第一个键
  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;
    }
}

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

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