请扮演一位后台开发者,使用PHP语言。现在有数据取出的数据 $visits=[ { "id": "1835", "clientsname": "石先生", "clientsphone": "15897401639", "user_id": "4", "user_name": "张巧儿", "avenues": "转介", "timestamp": "1685708928" }, { "id": "1833", "clientsname": "舒老大", "clientsphone": "18874569543", "user_id": "4", "user_name": "张巧儿", "avenues": "转介", "timestamp": "1685708977" }, { "id": "1834", "clientsname": "舒老", "clientsphone": "18874569543", "user_id": "4", "user_name": "张巧儿", "avenues": "自拓", "timestamp": "1685708978" }, { "id": "719", "clientsname": "张茂", "clientsphone": "18174563601", "user_id": "2", "user_name": "周蕾", "avenues": "自然来访", "timestamp": "1685761656" } ]timestamp是时间戳,需要按时间戳统计,把数组新组成[ { "date": "2023-06-02", "avenues": { "自然来访": 0, "转介": 2, "自拓": 1, "电call": 0, "老客户": 0 }, "张巧儿": { "自然来访": 0, "转介": 2, "自拓": 1, "电call": 0, "老客户": 0 } "dataCount": 3 }, { "date": "2023-06-03", "avenues": { "自然来访": 1, "转介": 0, "自拓": 0, "电call": 0, "老客户": 0 }, "周蕾": { "自然来访": 0, "转介": 2, "自拓": 0, "电call": 0, "老客户": 0 } "dataCount": 1 } ]
时间: 2023-08-18 14:09:43 浏览: 85
好的,根据你提供的数据,我们可以按照如下的步骤进行处理:
1. 对数据进行按时间戳排序;
2. 遍历数据,按照日期和用户分组,统计每个日期和每个用户的各渠道拓客数量;
3. 将结果按照日期和用户组成新的数组。
以下是代码实现:
```php
$visits=[
[
"id" => "1835",
"clientsname" => "石先生",
"clientsphone" => "15897401639",
"user_id" => "4",
"user_name" => "张巧儿",
"avenues" => "转介",
"timestamp" => "1685708928"
],
[
"id" => "1833",
"clientsname" => "舒老大",
"clientsphone" => "18874569543",
"user_id" => "4",
"user_name" => "张巧儿",
"avenues" => "转介",
"timestamp" => "1685708977"
],
[
"id" => "1834",
"clientsname" => "舒老",
"clientsphone" => "18874569543",
"user_id" => "4",
"user_name" => "张巧儿",
"avenues" => "自拓",
"timestamp" => "1685708978"
],
[
"id" => "719",
"clientsname" => "张茂",
"clientsphone" => "18174563601",
"user_id" => "2",
"user_name" => "周蕾",
"avenues" => "自然来访",
"timestamp" => "1685761656"
]
];
// 1. 按时间戳排序
usort($visits, function($a, $b) {
return $a['timestamp'] <=> $b['timestamp'];
});
// 2. 统计各渠道拓客数量
$results = [];
foreach ($visits as $visit) {
$date = date('Y-m-d', $visit['timestamp']);
$avenue = $visit['avenues'];
$user = $visit['user_name'];
if (!isset($results[$date])) {
$results[$date] = [
'date' => $date,
'avenues' => [
'自然来访' => 0,
'转介' => 0,
'自拓' => 0,
'电call' => 0,
'老客户' => 0,
],
'dataCount' => 0,
];
}
$results[$date]['avenues'][$avenue]++;
$results[$date][$user][$avenue]++;
$results[$date]['dataCount']++;
if (!isset($results[$date][$user])) {
$results[$date][$user] = [
'自然来访' => 0,
'转介' => 0,
'自拓' => 0,
'电call' => 0,
'老客户' => 0,
];
}
}
// 3. 组成新的数组
$newArray = [];
foreach ($results as $result) {
$newArray[] = $result;
}
```
这样就可以得到你需要的结果数组。
阅读全文