如何对重复数据的数组值求和
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何对重复数据的数组值求和相关的知识,希望对你有一定的参考价值。
我有一个数组有一些相同的ID
值,如下所示。
[
{"ID":"126871","total":"200.00","currency":"USD","name":"John"},
{"ID":"126872","total":"2000.00","currency":"Euro","name":"John"},
{"ID":"126872","total":"1000.00","currency":"Euro","name":"John"},
{"ID":"126872","total":"500.00","currency":"USD","name":"John"},
{"ID":"126872","total":"1000.00","currency":"Euro","name":"John"},
]
如果ID
值重复,则将相同currency
的总值相加。对于相同currency
的不同ID
,不需要总结total
。
这就是我想要的。
[
{"ID":"126871","total":"200.00","currency":"USD","name":"John"},
{"ID":"126872","total":"4000.00","currency":"Euro","name":"John"},
{"ID":"126872","total":"500.00","currency":"USD","name":"John"}
]
我遇到了上述问题。我已经尽力了。但是我得到了错误的结果。我非常感谢任何建议。
答案
@Cloud我已根据您的要求制作了功能并感谢@M。 I.查看这个总和部分。
$array = array(
array("ID" => "126871","total"=>"200.00","currency"=>"USD","name"=>"John"),
array("ID" => "126872","total"=>"2000.00","currency"=>"Euro","name"=>"John"),
array("ID" => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
array("ID" => "126872","total"=>"500.00","currency"=>"USD","name"=>"John"),
array("ID" => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
);
echo "<pre>";
print_r($array);
function unique_multidim_array($array, $key,$key1,$addedKey) {
$temp_array = array();
$i = 0;
$key_array = array();
$key1_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array) && !in_array($val[$key1], $key1_array)) {
$key_array[$i] = $val[$key];
$key1_array[$i] = $val[$key1];
$temp_array[$i] = $val;
}else{
$pkey = array_search($val[$key],$key_array);
$pkey1 = array_search($val[$key1],$key1_array);
if($pkey==$pkey1){
$temp_array[$pkey][$addedKey] += $val[$addedKey];
}else{
$key_array[$i] = $val[$key];
$key1_array[$i] = $val[$key1];
$temp_array[$i] = $val;
}
// die;
}
$i++;
}
return $temp_array;
}
$nArray = unique_multidim_array($array,"ID","currency","total");
// die;
print_r($nArray);
die;
另一答案
@Cloud试试这个。
$array = array(array("ID" => "126871","total"=>"200.00","currency"=>"USD","name"=>"John",),array("ID" => "126871","total"=>"200.00","currency"=>"USD","name"=>"John",),array("ID" => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John",));
echo "<pre>";
print_r($array);
$unique = array_map('unserialize', array_unique(array_map('serialize', $array)));
当然@Magnus Eriksson。我在解释为什么我们在步骤中使用'serialize'和'unserialize':
第1步:将多维数组转换为一维数组
要将多维数组转换为一维数组,首先要生成数组内所有元素(包括嵌套数组)的字节流表示。 serialize()函数可以生成一个值的字节流表示。要生成所有元素的字节流表示,请在serialize()函数内调用array_map()函数作为回调函数。无论多维数组有多少级别,结果都将是一维数组。
第2步:使值唯一
要使这个一维数组唯一,请使用array_unique()函数。
第3步:将其还原为多维数组
虽然数组现在是唯一的,但值看起来像字节流表示。要将其还原为多维数组,请使用unserialize()函数。
我希望我为什么要制作这段代码。
另一答案
内容:创建按“ID”和“货币”分组的数组。累计重复货币。
怎么样:
- 一次添加一行到输出数组。
- 如果group不在数组中,则添加它
- 如果它在数组中,则将货币添加到现有记录中。
码:
/** ----------------------------------------------
* Create an output array one row at a time.
*
* Group by Id and currency.
*
* @param array $groups
*
* @return array
*/
function getCurrencyGroups(array $groups)
{
$currencyGroups = array();
foreach ($groups as $item) {
$id = $item['ID'];
$currency = $item['currency'];
$amount = $item['total'];
if (!isset($currencyGroups[$id][$currency])) {
$currencyGroups[$id][$currency] = $amount;
}
else {
$currencyGroups[$id][$currency] += $amount;
}
}
return $currencyGroups;
}
运行:
$currencyGroups = getCurrencyGroups($source);
输出:
array (size=2)
126871 =>
array (size=1)
'USD' => string '200.00' (length=6)
126872 =>
array (size=2)
'Euro' => float 4000
'USD' => string '500.00' (length=6)
另一答案
你需要:
- 使用
json_decode
将您的json字符串转换为php数组。 - 循环遍历行并使用复合临时键对它们进行分组。换句话说,从每行的
ID
和currency
值生成一个字符串,并将该字符串用作临时唯一键。 - 对分组行的
total
值求和。 - 然后通过重新索引行并调用
json_encode()
来准备输出数组以返回到json。
代码:(Demo)
$json='[
{"ID":"126871","total":"200.00","currency":"USD","name":"John"},
{"ID":"126872","total":"2000.00","currency":"Euro","name":"John"},
{"ID":"126872","total":"1000.00","currency":"Euro","name":"John"},
{"ID":"126872","total":"500.00","currency":"USD","name":"John"},
{"ID":"126872","total":"1000.00","currency":"Euro","name":"John"}
]';
$array=json_decode($json,true); // convert to array
foreach($array as $row){
if(!isset($result[$row['ID'].$row['currency']])){
$result[$row['ID'].$row['currency']]=$row; // on first occurrence, store the full row
}else{
$result[$row['ID'].$row['currency']]['total']+=$row['total']; // after first occurrence, add current total to stored total
}
}
$result=json_encode(array_values($result)); // reindex the array and convert to json
echo $result; // display
输出:
[
{"ID":"126871","total":"200.00","currency":"USD","name":"John"},
{"ID":"126872","total":4000,"currency":"Euro","name":"John"},
{"ID":"126872","total":"500.00","currency":"USD","name":"John"}
]
以上是关于如何对重复数据的数组值求和的主要内容,如果未能解决你的问题,请参考以下文章