2
我有一個關聯數組,基本上是一個模擬數據庫的靜態虛擬數據。我試圖複製GROUPBY YEAR和TitleText在MySQL中所能做的事情,並且我可以使其在一個字段上工作 - 無論是TitleText還是YEAR使用array_reduce,但我無法弄清楚如何在兩個字段上進行操作。我需要每個YEAR和TitleText組合的一行來總和單位。這裏是數組:根據匹配的兩個其他值(php)在多維關聯數組中求和一個值
$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
)
)
我非常感謝你能提供任何幫助。
感謝 - 這ISN」這是我使用這個數組的唯一地方,所以我寧願不重做它,除非沒有別的選擇。當然,這些元素的順序無關緊要......我嘗試了你的代碼,而不是返回六個元素 - 每年一個/標題組合我每年只有兩個,但只有當年數組中的最後一個標題 - 這正是我每次嘗試它時發生的事情 - 我無法弄清楚如何讓它爲每個組合創建一個獨特的記錄。 – 2015-02-17 23:58:42
請使用print_r($ monthly_sales)的輸出編輯您的帖子; – 2015-02-18 00:00:55
@Regina McCreary我更新了代碼,現在檢查它,但如果你寫出print_r($ monthly_sales)的輸出結果會更好。 – 2015-02-18 00:10:16