2024-06-30 02:10:22
直接给你上代码和效果了。
建议1:合计最后一行可以让前端处理。
建议2:下次在发问题的时候,把所有场景的数组已文本的形式贴在问题里,不然在给你解决问题的过程中数据部分都得照着你的图片打出来(坑~)
效果图:
php代码:
<?php
$array = array(
array(
'create_time' => '2018-02-14 15:36:32',
'receivable_balance' => '200.00',
'name' => '李四',
'business_id' => 2
),
array(
'create_time' => '2018-02-21 15:36:31',
'receivable_balance' => '200.00',
'name' => '李四',
'business_id' => 2
),
array(
'create_time' => '2018-02-21 15:30:13',
'receivable_balance' => '-1000.00',
'name' => '王五',
'business_id' => 3
),
array(
'create_time' => '2018-02-27 15:30:13',
'receivable_balance' => '-1000.00',
'name' => '王五',
'business_id' => 3
),
array(
'create_time' => '2018-02-27 15:30:14',
'receivable_balance' => '-2000.00',
'name' => '王五',
'business_id' => 3
),
array(
'create_time' => '2018-02-28 15:30:14',
'receivable_balance' => '2000.00',
'name' => '王五',
'business_id' => 3
)
);
// 把所有的日期拿出来组合 table 标题
$dates = [];
foreach ($array as $key => $val) {
$date = date('Y-m-d', strtotime($val['create_time']));
if (!in_array($date, $dates)) {
array_push($dates, $date);
};
}
// 日期正序
sort($dates);
/**
* 组合新数组
*/
$listsArray = [];
$sumArray = [];
foreach ($array as $key => $val) {
$date = date('Y-m-d', strtotime($val['create_time']));
// 列表
if (isset($listsArray[$val['name']][$date])) {
// 如果某人一天多次进行累加
$listsArray[$val['name']][$date]['receivable_balance'] = bcadd($listsArray[$val['name']][$date]['receivable_balance'], $val['receivable_balance'], 2);
} else {
$listsArray[$val['name']][$date] = $val;
}
// 合计
if (!isset($sumArray[$date]['sum'])) { // 如果当天日期没有的设置为 0
$sumArray[$date]['sum'] = 0.00;
}
$sumArray[$date]['sum'] = bcadd($sumArray[$date]['sum'], $val['receivable_balance'], 2);
}
$table = "<table border='1'>";
// 标题行开始
$table .= "<tr>";
$table .= "<td>序号</td><td>业务员</td>";
foreach ($dates as $date) {
$table .= "<td>" . $date . "</td>";
}
$table .= "</tr>";
// 标题行结束
// 列表数据开始
$i = 1;
foreach ($listsArray as $name => $val) {
$table .= "<tr>";
$table .= "<td>" . $i . "</td><td>" . $name . "</td>";
foreach ($dates as $date) {
if (isset($val[$date])) {
$table .= "<td>" . $val[$date]['receivable_balance'] . "</td>";
} else {
$table .= "<td>" . "--" . "</td>";
}
}
$table .= "</tr>";
$i++;
}
// // 列表数据结束
// 合计数据开始
$table .= "<tr>";
$table .= "<td> </td><td>合计:</td>";
foreach ($dates as $date) {
$table .= "<td>" . $sumArray[$date]['sum'] . "</td>";
}
$table .= "</tr>";
// 合计数据结束
$table .= "</table>";
echo $table;
2024-06-30 11:09:18
我也知道啊~~现在是,我不知道怎么处理这个数组啊。需要处理才能使用,不处理,这个没办法使用,而且,要循环行和列的~~~那个列的日期,是循环出来的,根据查询结果