我有一个像这样的多维数组:
Array
(
[0] => array('id'=>1,'name'=>'Agent 1','total'=>3)
[1] => array('id'=>2,'name'=>'Agent 2','total'=>3)
[2] => array('id'=>3,'name'=>'Agent 3','total'=>3)
[3] => array('id'=>1,'name'=>'Agent 1','total'=>6)
)
我想从此数组中删除重复的代理,并将total
字段汇总到最终的数组中,如下所示:
Array
(
[0] => array('id'=>1,'name'=>'Agent 1','total'=>9)
[1] => array('id'=>2,'name'=>'Agent 2','total'=>3)
[2] => array('id'=>3,'name'=>'Agent 3','total'=>3)
)
我已经尝试了array_unique
,但它只删除了重复...
答案 0 :(得分:4)
试用此代码:sandbox code
算法的主要思想 - 在结果数组中缓存密钥对并进一步检查它们的存在。
[root@london r8169-6.023.02]# ls -lah
total 24K
drwxrwxrwx 3 root root 4.0K Sep 26 16:50 .
drwx------. 4 av av 4.0K Nov 28 16:21 ..
-rwxrwxrwx 1 root root 2.0K Oct 20 10:23 Makefile
-rwxrwxrwx 1 root root 4.4K Oct 20 10:23 readme
drwxrwxrwx 2 root root 4.0K Sep 26 16:50 src
[root@london r8169-6.023.02]#
答案 1 :(得分:0)
试试这个,
$arrays = array_values(array_combine(array_map(function ($i) { return $i['id']; }, $array), $array));
print_r($arrays);
<强> DEMO 强>
答案 2 :(得分:0)
为了获得您想要的确切输出,您需要一个嵌套循环。
$input = array(
0 => array('id'=>1,'name'=>'Agent 1','total'=>3),
1 => array('id'=>2,'name'=>'Agent 2','total'=>3),
2 => array('id'=>3,'name'=>'Agent 3','total'=>3),
3 => array('id'=>1,'name'=>'Agent 1','total'=>6)
);
// This is where we will save our result
$output = array();
foreach ($input as $item) {
// Used to determine if the current $item
// already exists in the $output array
$foundItem = false;
// Now we check the $item against every output entry
foreach ($output as &$entry) {
if ($entry["id"] == $item["id"]) {
// Since we found a match, let's add the
//current item's total to the output total.
$entry["total"] += $item["total"];
// Marking this as true will later prevent us
// from inserting the item to the output array twice
$foundItem = true;
}
}
// If the item was not found in the output array
// the $foundItem variable remains false
// Using ! to negate the boolean, we insert the item to the output array
if (!$foundItem) {
$output[] = $item;
}
}
意识到这不是获得所需输出的唯一方法。这只是最简单的解决方案,当然可以通过多种方式进行改进。但是,我会把那部分留给你。
答案 3 :(得分:0)
假设id是唯一的,您可以使用此
$array = array(
array('id' => 1, 'name' => 'Agent 1', 'total' => 3),
array('id' => 2, 'name' => 'Agent 2', 'total' => 3),
array('id' => 3, 'name' => 'Agent 3', 'total' => 3),
array('id' => 1, 'name' => 'Agent 1', 'total' => 6)
);
$array_ids = array();
foreach ($array as $key => $value) {
if (isset($array_ids[$value['id']])) {
$array[$array_ids[$value['id']]]['total'] += $value['total'];
unset($array[$key]);
}
else
{
$array_ids[$value['id']] = $key;
}
}
以这种方式将已使用的id保存到数组$ array_ids中,您可以轻松检查数组中是否已存在代理
答案 4 :(得分:0)
我尝试过使用array_reduce - 使用回调函数迭代地将数组减少为单个值。并根据要求编写了一个功能。希望这会对你有所帮助。
<?php
$array = [
0 => ['id' => 1, 'name' => 'Agent 1', 'total' => 3],
1 => ['id' => 2, 'name' => 'Agent 2', 'total' => 3],
2 => ['id' => 3, 'name' => 'Agent 3', 'total' => 3],
3 => ['id' => 1, 'name' => 'Agent 1', 'total' => 6],
];
$result = array_reduce($array, function($temp, $item){
isset($temp[$item['id']])
? $temp[$item['id']]['total'] += $item['total']
: $temp[$item['id']] = $item;
return $temp;
}, []);
print_r($result);
?>
<强>输出强>
阵列
(
[1] =&gt;数组([id] =&gt; 1 [名称] =&gt;代理商1 [总计] =&gt; 9)
[2] =&gt;数组([id] =&gt; 2 [名称] =&gt;代理商2 [总计] =&gt; 3)
[3] =&gt;数组([id] =&gt; 3 [名称] =&gt;代理商3 [总计] =&gt; 3)
)