# collection格式化计算两个数组的数据
有如下两组数据分别代表去年的营收和今年的营收,求每个月的盈亏情况。
```
$lastYear = [
6345.75,
9839.45,
7134.60,
9479.50,
9928.0,
8652.00,
7658.40,
10245.40,
7889.40,
3892.40,
3638.40,
2339.40
];
$thisYear = [
6145.75,
6895.00,
3434.00,
9349350,
9478.60,
7652.80,
4758.40,
10945.40,
3689.40,
8992.40,
7588.40,
2239.40
];
```
## 使用foreach
```
$profit = [];
foreach($thisYear as $key => $monthly){
$profit[$key] = $monthly - $lastYear[$key];
}
dd($profit);
```
## 使用 [zip](/collections/zip.md)、[first](/collections/first.md)和[last](collections/last.md)
```
$profit = collect($thisYear)->zip($lastYear)->map(function($monthly){
return $monthly->first() - $monthly->last();
});
dd($profit);
```
- 介绍
- Laravel5发送邮件使用Service隔离业务
- 如何使用Repository模式
- 如何使用Service模式
- 如何使用Presenter模式
- Laravel 5.* 执行迁移文件报错:Specified key was too long error
- EloquentORM关联关系
- EloquentORM关联关系之一对一
- EloquentORM关联关系之一对多
- EloquentORM关联关系之远层一对多
- EloquentORM关联关系之多对多
- EloquentORM关联关系之多态关联
- EloquentORM关联关系之多对多多态关联
- Laravel测试
- Laravel中涉及认证跳转地址的修改的地方
- Laravel中Collection的基本使用
- all
- avg
- chuck
- collapse
- combine
- contains
- containsStrict
- count
- diff
- diffAssoc
- diffKeys
- each
- every
- except
- filter
- first
- flatMap
- flatten
- flip
- forget
- forPage
- get
- groupBy
- has
- implode
- intersect
- intersectKey
- isEmpty
- isNotEmpty
- keyBy
- keys
- last
- map
- mapWithKeys
- max
- median
- merge
- min
- mode
- nth
- only
- partition
- pipe
- pluck
- pop
- prepend
- pull
- push
- put
- random
- reduce
- reject
- reverse
- search
- shift
- shuffle
- slice
- sort
- sortBy
- sortByDesc
- splice
- split
- sum
- take
- tap
- times
- toArray
- toJson
- transform
- union
- unique
- uniqueStrict
- values
- when
- where
- whereStrict
- whereIn
- whereInStrict
- whereNotIn
- whereNotInStrict
- zip
- Laravel中Collection的实际使用
- collection中sum求和
- collection格式化计算数据
- collection格式化计算数据计算github事件得分总和
- collection格式化markdown数据列表
- collection格式化计算两个数组的数据
- collection中reduce创建lookup数组
- TODO