我有一個名為$customerRecords. 我想通過客戶的電子郵件對這個陣列中的資料進行分組。
這是下面的陣列
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
},
]
這是我想要達到的預期結果,以便我可以知道屬于電子郵件的資料組。
{
"[email protected]": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
}
],
"[email protected]": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "[email protected]",
"phone": "9829484857"
}
]
}
]
}
所以這是我嘗試過的,但它不起作用:
return collect($customerRecords)->groupBy('customer.email')
uj5u.com熱心網友回復:
您幾乎完成了,只需定義客戶 0 專案,然后發送電子郵件
return collect($customerRecords)->groupBy('customer.0.email');
uj5u.com熱心網友回復:
這就是我能夠解決它的方法。
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514647.html
標籤:数组拉拉维尔收藏品
