下面是我的数组:
Array
(
[0] => Array
(
[transaction_id] => 1
[branch_id] => 4
[total] => 2254.35
)
[1] => Array
(
[transaction_id] => 1
[branch_id] => 4
[total] => 31989.2
)
[2] => Array
(
[transaction_id] => 3
[branch_id] => 5
[total] => 2109.71
)
[3] => Array
(
[transaction_id] => 1
[branch_id] => 4
[total] => 200
)
[4] => Array
(
[transaction_id] => 1
[branch_id] => 4
[total] => 200
)
[5] => Array
(
[transaction_id] => 2
[branch_id] => 6
[total] => 550
)
[6] => Array
(
[transaction_id] => 2
[branch_id] => 6
[total] => 550
)
[7] => Array
(
[transaction_id] => 1
[branch_id] => 4
[total] => 400
)
)
正如您所看到的,有多个具有相同键和相同值的数组。现在,我想合并transaction_id
键中具有相同值的数组。新阵列将是这样的:
Array
(
[0] => Array
(
[transaction_id] => 1
[branch_id] => 4 //always replace this element
[total] => 35043.55 //always sum this element(i.e. 2254.35 + 31989.2 + 200 + 200 + 400)
)
[1] => Array
(
[transaction_id] => 3
[branch_id] => 5
[total] => 2109.71
)
[2] => Array
(
[transaction_id] => 2
[branch_id] => 6
[total] => 1100 //i.e. 550 + 550
)
)
我该怎么做。我尝试使用array_merge_recursive()
,但没有运气。
答案 0 :(得分:2)
我有类似的任务要做,你真的必须为它做一个自定义代码。 我有一些简单的循环
$new_array=array();
Foreach ($array as $key=>$value) {
if (isset($new_array[$value[transaction_id]]){
//add sum and what you want to do
}else{
//add new record
}
}
这不是最好的,但它会做你想要的
并且不要忘记unset()
使用的行来保存数据
答案 1 :(得分:1)
简单,但诀窍。 (见评论)
$transactions = [
[
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200,
],
[
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200,
],
[
'transaction_id' => 2,
'branch_id' => 4,
'total' => 200,
],
[
'transaction_id' => 3,
'branch_id' => 4,
'total' => 200,
],
[
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200,
],
[
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200,
],
];
$merged = [];
foreach($transactions as $singleTransaction){
// Group via transaction_id and clone it for base data //
if(empty($merged[$singleTransaction['transaction_id']])){
$merged[$singleTransaction['transaction_id']] = $singleTransaction;
} else {
// Add up the totals of duplicate transaction_ids //
$merged[$singleTransaction['transaction_id']]['total'] += $singleTransaction['total'];
}
}
答案 2 :(得分:1)
我的直观解决方案:
$arr = Array
(
0 => Array
(
'transaction_id' => 1,
'branch_id' => 4,
'total' => 2254.35
),
1 => Array
(
'transaction_id' => 1,
'branch_id' => 4,
'total' => 31989.2
),
2 => Array
(
'transaction_id' => 3,
'branch_id' => 5,
'total' => 2109.71
),
3 => Array
(
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200
),
4 => Array
(
'transaction_id' => 1,
'branch_id' => 4,
'total' => 200
),
5 => Array
(
'transaction_id' => 2,
'branch_id' => 6,
'total' => 550
),
6 => Array
(
'transaction_id' => 2,
'branch_id' => 6,
'total' => 550
),
7 => Array
(
'transaction_id' => 1,
'branch_id' => 4,
'total' => 400
)
);
$transactionMap = [];
foreach ($arr as $item) {
$currentTransaction = $transactionMap[$item['transaction_id']];
$currentTransaction['branch_id'] = $item['branch_id'];
$currentTransaction['total'] = (isset($currentTransaction['total'])?$currentTransaction['total']:0)+$item['total'];
$transactionMap[$item['transaction_id']] = $currentTransaction;
}
答案 3 :(得分:1)
试试这个
$aTmp = array();
$aData = ...your data array..
$aRes = array();
foreach ($aData as $row) {
if (!isset($aTmp[$row['transaction_id']])) {
$aTmp[$row['transaction_id']] = $row;
} else {
$aTmp[$row['transaction_id']]['branch_id'] = $row['branch_id'];
$aTmp[$row['transaction_id']]['total'] += $row['total'];
}
}
$aRes = array_values($aTmp);
var_dump($aRes);
答案 4 :(得分:0)
假设所有数组都已准备好合并并嵌套在一个主数组中
定义临时数组以保存计算结果
$allArrays = [...]; // nest all arrays here
$matchedArrays = [];
foreach($allArrays as $id=>$toMerge) {
foreach($toMerge as $iid=>$data) {
if(isset($matchedArrays[$data['transaction_id']])) {
// process merging here
$matchedArrays[$data['transaction_id']] = [
'transaction_id' => $data['transaction_id'],
'branch_id' => $data['branch_id'],
'total' => $matchedArrays[$data['transaction_id']]['total'] + $data['total'],
];
} else {
$matchedArrays[$data['transaction_id']] = $data;
}
}
}
//结果数组将具有事务ID作为其键和合并的关联值