# collection格式化计算数据
有如下结构数组
```
$gates = [
'BaiYun_A_A17',
'BeiJing_J7',
'ShuangLiu_K203',
'HongQiao_A157',
'A2',
'BaiYun_B_B230'
];
```
请将它格式化后为如下数组:
```
$boards = [
'A17',
'J7',
'K203',
'A157',
'A2',
'B230'
];
```
## 使用foreach循环解决这个问题
```
$res = [];
foreach($gates as $key => $gate) {
if(strpos($gate, '_') === false) {
$res[$key] = $gate;
}else{
$offset = strrpos($gate, '_') + 1;
$res[$key] = mb_substr($gate , $offset);
}
}
var_dump($res);
```
## 使用collection中的[map](/collections/map.md)和PHP中的explode与end方法
```
collect($gates)->map(function($gate) {
$parts = explode('_', $gate);
return end($parts);
});
```
## 使用collection中的[map](/collections/map.md)、expload、[last](/collections/last.md)和toArray方法
```
collect($gates)->map(function($gate) {
return collect(explode('_', $gate))->last();
})->toArray();
```
- 介绍
- 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