基于匹配其他两个值(php)在多维关联数组中求和一个值

时间:2015-02-17 22:41:20

标签: php multidimensional-array associative-array

我有一个关联数组,它基本上是一个代表数据库的模型的静态虚拟数据。我试图复制GROUPBY年和TitleText将在MySQL中做什么,我可以使它在一个字段上工作 - 使用arrayText或YEAR使用array_reduce,但我无法弄清楚如何在两个字段上一起做。我需要为每个YEAR和TitleText组合使用一行来对单位求和。这是数组:

$monthly_sales = [
        [
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(7)["Month"], 
        'YEAR' => get_relative_date(7)["Year"], 
        'TitleText' => '1984', 
        'units' => 15 
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(7)["Month"], 
        'YEAR' => get_relative_date(7)["Year"],
        'TitleText' => 'Animal Farm', 
        'units' => 7 
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(7)["Month"], 
        'YEAR' => get_relative_date(7)["Year"],
        'TitleText' => 'Keep the Aspidistra Flying', 
        'units' => 5 
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(6)["Month"], 
        'YEAR' => get_relative_date(6)["Year"],
        'TitleText' => 'Animal Farm', 
        'units' => 6 
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(6)["Month"], 
        'YEAR' => get_relative_date(6)["Year"],
        'TitleText' => 
        'Keep the Aspidistra Flying', 
        'units' => 6 
        ], [ 
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(6)["Month"],
        'YEAR' => get_relative_date(6)["Year"],
        'TitleText' => '1984', 
        'units' => 2  
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(5)["Month"], 
        'YEAR' => get_relative_date(5)["Year"],
        'TitleText' => 'Keep the Aspidistra Flying', 
        'units' => 5  
        ], [ 
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(5)["Month"], 
        'YEAR' => get_relative_date(5)["Year"], 
        'TitleText' => '1984', 
        'units' => 3 
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(5)["Month"], 
        'YEAR' => get_relative_date(5)["Year"], 
        'TitleText' => 'Animal Farm', 
        'units' => 3  
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(4)["Month"], 
        'YEAR' => get_relative_date(4)["Year"],
        'TitleText' => 'Animal Farm', 
        'units' => 4  
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(4)["Month"], 
        'YEAR' => get_relative_date(4)["Year"], 
        'TitleText' => 'Keep the Aspidistra Flying', 
        'units' => 4  
        ], [ 
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(4)["Month"], 
        'YEAR' => get_relative_date(4)["Year"], 
        'TitleText' => '1984', 
        'units' => 3  
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(3)["Month"], 
        'YEAR' => get_relative_date(3)["Year"], 
        'TitleText' => 'Animal Farm', 
        'units' => 10 
        ], [ 
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(3)["Month"], 
        'YEAR' => get_relative_date(3)["Year"],
        'TitleText' => '1984', 
        'units' => 4  
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(3)["Month"], 
        'YEAR' => get_relative_date(3)["Year"], 
        'TitleText' => 'Keep the Aspidistra Flying', 
        'units' => 1  
        ], [ 
        'XML_KEY' => '9990', 
        'MONTH' => get_relative_date(1)["Month"], 
        'YEAR' => get_relative_date(1)["Year"], 
        'TitleText' => 'Animal Farm', 
        'units' => 9  
        ], [ 
        'XML_KEY' => '9991', 
        'MONTH' => get_relative_date(1)["Month"], 
        'YEAR' => get_relative_date(1)["Year"], 
        'TitleText' => 'Keep the Aspidistra Flying', 
        'units' => 3  
        ], [ 
        'XML_KEY' => '9992', 
        'MONTH' => get_relative_date(1)["Month"], 
        'YEAR' => get_relative_date(1)["Year"], 
        'TitleText' => '1984', 
        'units' => 1  
        ]
    ];

以下是我想要实现的输出:

Array
(
    [0] => Array
        (

            [YEAR] => 2014
            [TitleText] => 1984
            [units] => 27
        )

    [1] => Array
        (
            [YEAR] => 2014
            [TitleText] => Animal Farm
            [units] => 30
        )

    [2] => Array
        (
            [YEAR] => 2014
            [TitleText] => Keep the Aspidistra Flying
            [units] => 21
        )
    [3] => Array
        (

            [YEAR] => 2015
            [TitleText] => 1984
            [units] => 1
        )

    [4] => Array
        (
            [YEAR] => 2015
            [TitleText] => Animal Farm
            [units] => 9
        )

    [5] => Array
        (
            [YEAR] => 2015
            [TitleText] => Keep the Aspidistra Flying
            [units] => 3
        )
)

我非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您编写的数组就像您想要的输出更好,我想您的每月销售量就像是:

Array
(
    [0] => Array
        (

            [YEAR] => 2014
            [TitleText] => 1984
            [units] => 27
            [XML_KEY] => 9992
            [MONTH] => 10
               ) 
)

代码:

    $info = array();
    $final = array();

    for($i = 0; $i < count($monthly_sales); $i++) {
    $year = $monthly_sales[$i]['YEAR'];
    $title = $monthly_sales[$i]['TitleText'];

    $info[$year][$title] += $monthly_sales[$i]['units'];
    }

    $i = 0;
    foreach ($info as $year => $data) {
      foreach($data as $title => $units) {
        $final[$i]['Year'] = $year;
        $final[$i]['TitleText'] = $title;
        $final[$i]['units'] = $units;
        $i++;
      }
    }

    print_r($final);