根据匹配的其他两个值 (php) 对多维关联数组中的一个值求和

Sum one value in a multidimensional associative array based on matching two other values (php)

我有一个关联数组,它本质上是代表数据库的实体模型的静态虚拟数据。我正在尝试复制 GROUPBY YEAR AND TitleText 在 MySQL 中所做的事情,我可以让它在一个字段上工作 - 使用 array_reduce 的 TitleText 或 YEAR 但我不知道如何在两个领域在一起。每个 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
        )
)

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

如果你把你的数组写成你想要的输出更好,我想你的数组 $monthly_sales 就像:

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);