php如何将数组中id相同数据的value相加之后得到新的数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php如何将数组中id相同数据的value相加之后得到新的数组相关的知识,希望对你有一定的参考价值。
array (size=2)
'id' => string '404' (length=3)
'name' => string 'a' (length=1)
'value' => string '100.00' (length=6)
array (size=2)
'id' => string '404' (length=3)
'name' => string 'a' (length=1)
'value' => string '100.00' (length=6)
我需要的数组格式是
$a = array(
0 => array(
'id'=>1,
'value'=>4,
),
);
id相同的value值相加得出一个合。然后再生成一个下方那样的数组。求大神代码支援,小弟感激不尽
$a = array(
0 => array(
'id'=>1,
'value'=>4,
),
1 => array(
'id'=>2,
'value'=>2,
),
2 => array(
'id'=>3,
'value'=>3,
),
3 => array(
'id'=>4,
'value'=>5,
),
4 => array(
'id'=>5,
'value'=>1,
),
);
$b = array(
0 => array(
'id'=>1,
'value'=>2,
),
1 => array(
'id'=>2,
'value'=>3,
),
2 => array(
'id'=>3,
'value'=>2,
),
3 => array(
'id'=>4,
'value'=>3,
),
4 => array(
'id'=>5,
'value'=>2,
),
);
$c = array();
foreach($a as $k=>$v)
if ($v['id'] == $b[$k]['id'])
$c[] = array('id'=>$v['id'],'count'=>$v['value'] + $b[$k]['value']);
echo '<pre>';
print_r($c); 参考技术B // php5.5+
$ary1 = ['a'=>1,'b'=>2,'c'=>3];
$ary2 = ['a'=>1,'b'=>2,'d'=>5];
$merge = function($ary1,$ary2)
foreach($ary2 as $k=>$v)
if(key_exists($k, $ary1))
$ary1[$k] += $v;
else
$ary1[$k] = $v;
return $ary1;
;
var_dump( $merge($ary1,$ary2) );
在 Laravel、Blade 或 PHP 中按键相加每个数组子项的总量
【中文标题】在 Laravel、Blade 或 PHP 中按键相加每个数组子项的总量【英文标题】:Adding up the total amount of each array child item by key in Laravel, Blade or PHP 【发布时间】:2017-07-03 02:16:50 【问题描述】:我正在尝试将每个收据/父母每个 receiptitems->value
的总金额相加。
理想的结果是循环显示收据;
receipt->id
receipt->merchant
receipt->totalValue
每个子值的总值(即整个收据值)。
示例 JSON 输出:
[
"id":1,
"user_id":1,
"image":"58a31ba406d16.jpg",
"merchant":"Tesco",
"created_at":"2016-04-23 22:02:58",
"receiptitems":[
"receipt_id":1,
"category_id":2,
"value":5285
,
"receipt_id":1,
"category_id":5,
"value":5231
,
"receipt_id":1,
"category_id":5,
"value":6822
]
,
"id":2,
"user_id":1,
"image":"58a31ba4073c8.jpg",
"merchant":"Tesco",
"created_at":"2016-09-19 02:55:38",
"receiptitems":[
"receipt_id":2,
"category_id":2,
"value":7009
,
"receipt_id":2,
"category_id":7,
"value":2147
]
,
"id":3,
"user_id":1,
"image":"58a31ba407a26.jpg",
"merchant":"Tesco",
"created_at":"2016-08-06 12:30:27",
"receiptitems":[
"receipt_id":3,
"category_id":7,
"value":4598
,
"receipt_id":3,
"category_id":2,
"value":8799
,
"receipt_id":3,
"category_id":3,
"value":773
]
,
"id":4,
"user_id":1,
"image":"58a31ba407e69.jpg",
"merchant":"Tesco",
"created_at":"2016-12-10 21:21:43",
"receiptitems":[
"receipt_id":4,
"category_id":5,
"value":883
,
"receipt_id":4,
"category_id":5,
"value":3013
,
"receipt_id":4,
"category_id":2,
"value":1013
,
"receipt_id":4,
"category_id":1,
"value":3243
]
,
"id":5,
"user_id":1,
"image":"58a31ba408187.jpg",
"merchant":"Tesco",
"created_at":"2017-01-18 14:12:16",
"receiptitems":[
"receipt_id":5,
"category_id":7,
"value":1355
,
"receipt_id":5,
"category_id":1,
"value":1547
,
"receipt_id":5,
"category_id":1,
"value":1640
,
"receipt_id":5,
"category_id":1,
"value":2466
,
"receipt_id":5,
"category_id":8,
"value":2689
]
]
我一直在尝试使用控制器以及刀片模板,但无法获得所需的结果。我一直在看 array_sum() http://php.net/manual/en/function.array-sum.php 但我不确定如何实现它。
我已经在 RecriptsController 中尝试过:
$receipts = Receipt::where('user_id', Auth::id())
->with('receiptitems')
->get();
foreach ($receipts as $receipt)
$result = array_sum($receipt->receiptitems->value);
;
return $result;
而且在 Blade 模板中:
@foreach($receipts as $receipt)
<li> $receipt->merchant </li>
<ul>
@foreach($receipt->receiptitems as $item)
<li> array_sum($item->value) </li>
@endforeach
</ul>
@endforeach
【问题讨论】:
【参考方案1】:通常,您希望array_column
与array_sum
结合使用:
foreach ($receipts as $receipt)
$total = array_sum(array_column($receipt['receiptitems'], 'value'));
print_r($total);
array_column
从receiptitems
子数组中取出value
键。然后array_sum
将这些加起来。但是你有一个对象的集合,所以你实际上需要一个基于array_map
的稍微不同的方法:
foreach ($receipts as $receipt)
$total = array_sum(array_map(function ($o) return $o->value; , $receipt->receiptitems));
print_r($total);
顺便说一句,您可能需要考虑一个Receipt::getTotalAttribute()
mutator,它将返回项目的总数并简化您与模型交互的方式。
【讨论】:
感谢您的回答。我收到array_column() expects parameter 1 to be array, object given
错误
听起来像一个集合。也许您想使用(array)
将其转换为数组,或者使用->toArray()
或等效的转换方法。
我在循环之外添加了return $total->toArray();
,但我仍然得到同样的错误(参考你的例子中的第二行)。【参考方案2】:
虽然问题得到了回答,我得到了我想要的结果。一位朋友帮助我选择了另一个也有效的选项(收藏):
public function index()
$receipts = Receipt::where('user_id', Auth::id())
->with('receiptitems')
->get();
$collection = collect($receipts);
$data = $collection->map(
function($receipt)
$r = new stdClass;
$r->id = $receipt->id;
$r->merchant = $receipt->merchant;
$r->amount = collect($receipt->receiptitems)->pluck('value')->sum();
$r->date = $receipt->date;
return $r;
);
return view('transactions', compact('data'));
希望对其他人有所帮助。
【讨论】:
以上是关于php如何将数组中id相同数据的value相加之后得到新的数组的主要内容,如果未能解决你的问题,请参考以下文章
PHP/SQL - 如果 foreach() 中的 ID 相同,则将值相加
如何在EXCEL里面将相同编号的数值相加,不同编号的数值保留。求具体算法。