动态集合映射:在集合中添加缺失的记录

问题描述

我有以下内容

->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

来源:脸书 客户 : 是 计数:227

来源:脸书 客户 : 没有 计数:0

来源:Whatsapp 客户 : 没有 计数:254

来源:Whatsapp 客户 : 是 计数:0

解决方法

使用 Laravel 的集合助手,您可以在原始集合中添加缺失的数据

转型

/** Result from original query */
$collection = collect([
    ["Source"=>"Facebook","Customer"=> "Yes","count"=> 227],["Source"=>"PinInterest","count"=> 370],"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
        )

)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...