动态集合映射:在集合中添加缺失的记录
Dynamic Collection Mapping: Add missing records in collection
我有以下
->select(DB::raw('source as Source, customer as Customers, COUNT(*) as count'))
->groupBy('source', 'customer')
->get();
我得到以下结果
Illuminate\Support\Collection {#460 ▼
#items: array:4 [▼
0 => {#466 ▼
+"Source": "Facebook"
+"Customer": "Yes"
+"count": 227
}
1 => {#463 ▼
+"Source": "PinInterest"
+"Customer": "Yes"
+"count": 370
}
2 => {#465 ▼
+"Source": "PinInterest"
+"Customer": "No"
+"count": 133
}
3 => {#467 ▼
+"Source": "Whatsapp"
+"Customer": "No"
+"count": 254
}
]
}
现在来源可以是 1 - 10 个不同的渠道,具体取决于客户。
现在客户是或否。
如何添加修改集合以添加源 Facebook、客户编号和计数 0,以及源 WhatsApp、客户是和计数 0
资料来源:Facebook
顾客:是的
计数:227
资料来源:Facebook
顾客:没有
计数:0
来源:Whatsapp
顾客:没有
计数:254
来源:Whatsapp
顾客:是的
计数:0
使用 laravel 的收集助手,您可以将原始集合中缺失的数据添加为
转型
/** Result from original query */
$collection = collect([
["Source"=>"Facebook","Customer"=> "Yes","count"=> 227],
["Source"=>"PinInterest","Customer"=> "Yes","count"=> 370],
["Source"=>"PinInterest","Customer"=>"No","count"=> 133],
["Source"=>"Whatsapp","Customer"=> "No","count"=>254]
]);
/** Unique list of sources */
$sources = $collection->pluck('Source')->unique();
/** Unique list of customers */
$customers = collect(["Yes","No","May Be"]);
$sources->each(function ($source, $sourceKey) use (&$collection,$customers) {
if($collection->where('Source', $source)->count() < count($customers)){
$customers->each(function ($customer, $customerKey) use (&$collection,$source) {
if($collection->where('Source', $source)->where('Customer', $customer)->count() === 0){
$collection = $collection->merge([["Source"=>$source,"Customer"=> $customer,"count"=>0]]);
}
});
}
});
/** Sort and print */
echo "<pre>";
print_r($collection->sortBy('Source')->toArray());
echo "</pre>";
输出
Array
(
[0] => Array
(
[Source] => Facebook
[Customer] => Yes
[count] => 227
)
[4] => Array
(
[Source] => Facebook
[Customer] => No
[count] => 0
)
[5] => Array
(
[Source] => Facebook
[Customer] => May Be
[count] => 0
)
[1] => Array
(
[Source] => PinInterest
[Customer] => Yes
[count] => 370
)
[2] => Array
(
[Source] => PinInterest
[Customer] => No
[count] => 133
)
[6] => Array
(
[Source] => PinInterest
[Customer] => May Be
[count] => 0
)
[3] => Array
(
[Source] => Whatsapp
[Customer] => No
[count] => 254
)
[7] => Array
(
[Source] => Whatsapp
[Customer] => Yes
[count] => 0
)
[8] => Array
(
[Source] => Whatsapp
[Customer] => May Be
[count] => 0
)
)
我有以下
->select(DB::raw('source as Source, customer as Customers, COUNT(*) as count'))
->groupBy('source', 'customer')
->get();
我得到以下结果
Illuminate\Support\Collection {#460 ▼
#items: array:4 [▼
0 => {#466 ▼
+"Source": "Facebook"
+"Customer": "Yes"
+"count": 227
}
1 => {#463 ▼
+"Source": "PinInterest"
+"Customer": "Yes"
+"count": 370
}
2 => {#465 ▼
+"Source": "PinInterest"
+"Customer": "No"
+"count": 133
}
3 => {#467 ▼
+"Source": "Whatsapp"
+"Customer": "No"
+"count": 254
}
]
}
现在来源可以是 1 - 10 个不同的渠道,具体取决于客户。 现在客户是或否。
如何添加修改集合以添加源 Facebook、客户编号和计数 0,以及源 WhatsApp、客户是和计数 0
资料来源:Facebook 顾客:是的 计数:227
资料来源:Facebook 顾客:没有 计数:0
来源:Whatsapp 顾客:没有 计数:254
来源:Whatsapp 顾客:是的 计数:0
使用 laravel 的收集助手,您可以将原始集合中缺失的数据添加为
转型
/** Result from original query */
$collection = collect([
["Source"=>"Facebook","Customer"=> "Yes","count"=> 227],
["Source"=>"PinInterest","Customer"=> "Yes","count"=> 370],
["Source"=>"PinInterest","Customer"=>"No","count"=> 133],
["Source"=>"Whatsapp","Customer"=> "No","count"=>254]
]);
/** Unique list of sources */
$sources = $collection->pluck('Source')->unique();
/** Unique list of customers */
$customers = collect(["Yes","No","May Be"]);
$sources->each(function ($source, $sourceKey) use (&$collection,$customers) {
if($collection->where('Source', $source)->count() < count($customers)){
$customers->each(function ($customer, $customerKey) use (&$collection,$source) {
if($collection->where('Source', $source)->where('Customer', $customer)->count() === 0){
$collection = $collection->merge([["Source"=>$source,"Customer"=> $customer,"count"=>0]]);
}
});
}
});
/** Sort and print */
echo "<pre>";
print_r($collection->sortBy('Source')->toArray());
echo "</pre>";
输出
Array
(
[0] => Array
(
[Source] => Facebook
[Customer] => Yes
[count] => 227
)
[4] => Array
(
[Source] => Facebook
[Customer] => No
[count] => 0
)
[5] => Array
(
[Source] => Facebook
[Customer] => May Be
[count] => 0
)
[1] => Array
(
[Source] => PinInterest
[Customer] => Yes
[count] => 370
)
[2] => Array
(
[Source] => PinInterest
[Customer] => No
[count] => 133
)
[6] => Array
(
[Source] => PinInterest
[Customer] => May Be
[count] => 0
)
[3] => Array
(
[Source] => Whatsapp
[Customer] => No
[count] => 254
)
[7] => Array
(
[Source] => Whatsapp
[Customer] => Yes
[count] => 0
)
[8] => Array
(
[Source] => Whatsapp
[Customer] => May Be
[count] => 0
)
)