# collection中reduce創建lookup數組
有如下數組
```
$employees = [
[
'name' => 'example',
'email' => 'example@exmaple.com',
'company' => 'example Inc.'
],
[
'name' => 'Lucy',
'email' => 'lucy@example.com',
'company' => 'ibm Inc.'
],
[
'name' => 'Taylor',
'email' => 'toylor@laravel.com',
'company'=>'Laravel Inc.'
]
];
```
通過計算將其轉化成如下格式
```
$lookup = [
'example' => 'example@example.com',
'Lucy' => ‘lucy@example.com’,
'Taylor'=> 'toylor@laravel.com'
];
```
## 使用foreach解決
```
$emails = [];
foreach ($employees as $key => $value) {
$emails[$value['name']] = $value['email'];
}
dd($emails);
```
## 使用collection的[reduce](/collections/reduce.md)方法
```
$emails = collect($employees)->reduce(function($emailLookup, $employee){
$emailLookup[$employee['name']] = $employee['email'];
return $emailLookup;
},[]);
dd($emails);
```
## 使用 [pluck](/collections/pluck.md) 方法
```
$emails = collect($employees)->pluck('name', 'email');
dd($emails);
```
- 介紹
- 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