按id列分组2D数组,将amount推入Group,并将amount添加到Group中所有先前的amount



使用array_key_exists()可以找到重复的值并将它们相加。在我的例子中,我并没有试图把所有的值加起来,并把它们合并成一个值。我要做的是迭代这些值,按ac_no值分组,然后每次遇到相同的ac_no值,将新的amount添加到组中以前遇到的所有数量。

下面是我的示例输入:

$array = [
['ac_no' => 100001, 'amount' => 0.00],
['ac_no' => 100001, 'amount' => 51255.11],
['ac_no' => 100001, 'amount' => -500.00],
['ac_no' => 100001, 'amount' => -621.05],
['ac_no' => 100002, 'amount' => .00],
['ac_no' => 100003, 'amount' => .00],
['ac_no' => 100004, 'amount' => 20714.00],
['ac_no' => 100004, 'amount' => 0.00]
];

注意ac_no100001出现了4次。在结果中,100001组应该有以下元素,因为每个绝对值都是加到组中所有先前的值上的:

[
52,376.05,    # .00 + 51255.11 + abs(-500.00) + abs(-621.05)
52,376.05,    # 51255.11 + abs(-500.00) + abs(-621.05)
1,120.94,     # abs(-500.00) + abs(-621.05)
621.05        # abs(-621.05)
]

我当前的编码尝试看起来像这样:

foreach ($nameAndCode as $key => $vals) {
if (array_key_exists($vals['ac_no'], $res)) {
$amt = abs($vals['amount']);
$res[$vals['ac_no']]['ac_no']  += $vals['ac_no'];
$res[$vals['ac_no']]['amount'] += $amt; 
} else {
$res[$vals['ac_no']]  = $vals;
}
}

三个foreach()以及array_sum()is_array()将完成这项工作(我正在考虑减去负值):

$acNoWiseArray = [];

//get all amounts as an array  ac_no wise
foreach($array as $arr){
$acNoWiseArray[$arr['ac_no']]['sum'][] = $arr['amount'];
}


$finalArray = [];
foreach($acNoWiseArray as $key=>&$value){//calling by reference
foreach($value['sum'] as $k=> $val){
$finalArray[$key]['new_sum'][] = (is_array($value)) ? array_sum($value['sum']) : $value['sum'][$k];
array_shift($value['sum']);
}
}

print_r($finalArray);

输出:https://3v4l.org/duoEc

重要更新:但是当我仔细观察你的计算(基于@mickmackusa评论)时,我发现你忽略了-符号并将所有值相加为正。要做到这一点,请执行以下操作:

$acNoWiseArray = [];

foreach($array as $arr){
$acNoWiseArray[$arr['ac_no']]['sum'][] = $arr['amount'];
}


$finalArray = [];
foreach($acNoWiseArray as $key=>&$value){
foreach($value['sum'] as $k=> $val){
$finalArray[$key]['new_sum'][] = (is_array($value)) ? array_sum(array_map('abs',$value['sum'])) : abs($value['sum'][$k]);
array_shift($value['sum']);
}


}

print_r($finalArray);

输出:https://3v4l.org/j9tkC

<?php
$result = array();
$nameAndCode = array(
array(
'ac_no' => 100001,
'amount' => .00
),
array(
'ac_no' => 100001,
'amount' => 51255.11
),
array(
'ac_no' => 100001,
'amount' => -500.00
),
array(
'ac_no' => 100001,
'amount' => -621.05
),
array(
'ac_no' => 100002,
'amount' => .00
),
array(
'ac_no' => 100003,
'amount' => .00
),
array(
'ac_no' => 100004,
'amount' => 20714.00
),
array(
'ac_no' => 100004,
'amount' => .00
),
);

// init new array of arrays to store each amount of ac_no seprately
$parsedArr = array();
foreach ($nameAndCode as $_arr) {
// iterate over data source 
if (!isset($parsedArr[$_arr['ac_no']])) $parsedArr[$_arr['ac_no']] = array();
// save to new, convenient array
array_push($parsedArr[$_arr['ac_no']], $_arr['amount']);
}
// iterate over parsed array where each element is an array of values
foreach ($parsedArr as $ac_no => $values) {
// check if index in resulting array exist
if (!isset($result[$ac_no])) $result[$ac_no] = array();
// prep all_values sum index
if (!isset($result[$ac_no]['ALL_VALUES'])) $result[$ac_no]['ALL_VALUES'] = 0;
// iterate over each values in ac_no array; set iterator to current key and itarate at max 3 elements ahead
foreach ($values as $key => $val) {
for ($i = $key; $i < $key + 4; $i++) {
// check if element exists
if (!isset($result[$ac_no][$key])) $result[$ac_no][$key] = 0;
// check if element exists
if (isset($values[$i])) $result[$ac_no][$key] += abs($values[$i]);
}
$result[$ac_no]['ALL_VALUES'] += abs($val);
}
}
print_r($result);
  • 在迭代每一行时,无条件地将一个新值压入值为0的组
  • 然后使用abs()强制量值为正。
  • 然后迭代组中的所有元素,通过引用修改值,并将每个元素增加当前的无符号量。

代码:(演示)

$array = [
['ac_no' => 100001, 'amount' => 0.00],
['ac_no' => 100001, 'amount' => 51255.11],
['ac_no' => 100001, 'amount' => -500.00],
['ac_no' => 100001, 'amount' => -621.05],
['ac_no' => 100002, 'amount' => .00],
['ac_no' => 100003, 'amount' => .00],
['ac_no' => 100004, 'amount' => 20714.00],
['ac_no' => 100004, 'amount' => 0.00]
];

$result = [];
foreach ($array as $row) {
$result[$row['ac_no']][] = 0;
$amount = abs($row['amount']);
foreach ($result[$row['ac_no']] as &$cached) {
$cached += $amount;
}
}
var_export($result);

输出:

array (
100001 => array (
0 => 52376.16,
1 => 52376.16,
2 => 1121.05,
3 => 621.05,
),
100002 => array (
0 => 0.0,
),
100003 => array (
0 => 0.0,
),
100004 => array (
0 => 20714.0,
1 => 0.0,
),
)

相关内容

  • 没有找到相关文章

最新更新