我有一个像这样的dinamic值的数组。目标是合并数组,具有相同的代码和具有相同代码的值的总和。
$array = array(
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'10',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'15',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'15',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'10',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'5',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'15',
),
),
);
我想要这样的结果
$result = array(
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'30',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'35',
),
),
);
结果只有代码AA和BB,因为代码AC不存在于第一个索引上。然后将相同的值编码为总和。
由于
答案 0 :(得分:2)
2
答案 1 :(得分:1)
简单,这是一种方式
<?php
$codes = array_column($array[0],"code");
$out =array();
foreach($array as $main)
{
foreach($main as $sub)
{
if(in_array($sub['code'], $codes))
{
if(isset($out[$sub['code']]))
{
$out[$sub['code']]['cost']+=$sub['cost'];
}else
{
$out[$sub['code']] = $sub;
}
}
}
}
print_r(array_values($out));
?>
测试结果
akshay@db-3325:/tmp$ cat test.php
<?php
$array = array(
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'10',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'15',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'15',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'10',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'5',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'15',
),
),
);
$codes = array_column($array[0],"code");
$out =array();
foreach($array as $main)
{
foreach($main as $sub)
{
if(in_array($sub['code'], $codes))
{
if(isset($out[$sub['code']]))
{
$out[$sub['code']]['cost']+=$sub['cost'];
}else
{
$out[$sub['code']] = $sub;
}
}
}
}
print_r(array_values($out));
?>
<强>输出强>
akshay@db-3325:/tmp$ php test.php
Array
(
[0] => Array
(
[code] => AA
[name] => A Name
[cost] => 30
)
[1] => Array
(
[code] => AB
[name] => B Name
[cost] => 35
)
)
答案 2 :(得分:0)
<?php
ini_set("display_errors", 1);
$array = array(
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'10',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'15',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'15',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'10',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'5',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'15',
),
),
);
$newData=array();
array_map(function($arrayData) use(&$newData){
foreach($arrayData as $data => $value)
{
$newData[]=$value;
}
}, $array);
$result=array();
array_map(function($data) use(&$result){
// array("AA","AB") in this array you can add your code for which you want to merge.
if(in_array($data["code"], array("AA","AB")))
{
if(!isset($result[$data["code"]]))
{
$result[$data["code"]]=$data;
}
else
{
$result[$data["code"]]["cost"]+=$data["cost"];
}
}
}, $newData);
$result=array(array_values($result));
print_r($result);
<强>输出:强>
Array
(
[0] => Array
(
[0] => Array
(
[code] => AA
[name] => A Name
[cost] => 30
)
[1] => Array
(
[code] => AB
[name] => B Name
[cost] => 35
)
)
)
答案 3 :(得分:0)
<强>已更新强>
<?php
$collection = array(
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'10',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'15',
),
array(
'code'=>'AD',
'name'=>'D Name',
'cost'=>'45',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'15',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'10',
),
),
array(
array(
'code'=>'AA',
'name'=>'A Name',
'cost'=>'5',
),
array(
'code'=>'AB',
'name'=>'B Name',
'cost'=>'10',
),
array(
'code'=>'AC',
'name'=>'C Name',
'cost'=>'15',
),
),
);
$newcollection=array();
$keysregistered=array();
$i=1;
foreach($collection as $list) {
$collectionkeys = array();
foreach($list as $arr) {
$key=$arr['code'];
$collectionkeys[]=$key;
if(isset($keysregistered[$key])) {
$oldtotal = $keysregistered[$key];
$total = $oldtotal+$arr['cost'];
$newcollection[$key]['cost'] = $total;
} else {
if($i==1) {
$newcollection[$key] = $arr;
$keysregistered[$key] = $arr['cost'];
}
}
}
$registeredkeys = array_keys($keysregistered);
$diff = array_diff($registeredkeys,$collectionkeys);
foreach($diff as $delkey) {
unset($keysregistered[$delkey],$newcollection[$delkey]);
}
$i++;
}
$finalarr = array_values($newcollection);
print_r($finalarr);
?>
现在代码已更新,它甚至会从第一个项目中删除AD,并为您提供唯一代码的总计。
答案 4 :(得分:0)
使用array_column
,array_map
,call_user_func_array
和array_merge
函数的解决方案:
// $arr is your initial array
$merged = call_user_func_array("array_merge", $arr);
// getting `code` keys from the first nested item
$result = array_map(function($v){
return [];
}, array_column($arr[0], 'cost', 'code'));
foreach ($merged as $item) {
if (isset($result[$item['code']])) {
if ($result[$item['code']]) {
$result[$item['code']]['cost'] += $item['cost'];
} else {
$result[$item['code']] = $item;
}
}
}
$result = [array_values($result)];
print_r($result);
输出:
Array
(
[0] => Array
(
[0] => Array
(
[code] => AA
[name] => A Name
[cost] => 30
)
[1] => Array
(
[code] => AB
[name] => B Name
[cost] => 35
)
)
)