在 Laravel、Blade 或 PHP 中按键相加每个数组子项的总量
Posted
技术标签:
【中文标题】在 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'));
希望对其他人有所帮助。
【讨论】:
以上是关于在 Laravel、Blade 或 PHP 中按键相加每个数组子项的总量的主要内容,如果未能解决你的问题,请参考以下文章
Welcome.blade.php 在 laravel 中显示空白
.BLADE.PHP 在我的 LARAVEL 项目中不起作用。 .PHP 在我看来运行良好