一、因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索。三表合并后的数组结构如下:
Array
(
[0] => Array
(
[history_id] => 12
[sla_group_id] => 1
[sla_id] => -1
[create_time] => 1513057695
[tasklog_id] => 12
[tasklog_time] => 2017-12-12 13:48:15
[tasklog_name] => window_2008
[tasklog_type] => 分组立即调度
[tasklog_user] => admin
[tasklog_status] => 3
)
[1] => Array
(
[history_id] => 11
[sla_group_id] => 1
[sla_id] => 1
[create_time] => 1513057563
[tasklog_id] => 11
[tasklog_time] => 2017-12-12 13:46:03
[tasklog_name] => centos7_USHARK
[tasklog_type] => 一小时备份频率
[tasklog_user] => 系统自动
[tasklog_status] => 3
)
[2] => Array
(
[history_id] => 19
[sla_group_id] => 1
[sla_id] => 98
[create_time] => 1513059714
[tasklog_id] => 19
[tasklog_time] => 2017-12-12 14:21:54
[tasklog_name] => huawei_fusion_backup
[tasklog_type] => 华为虚拟化备份
[tasklog_user] => ushark.net
[tasklog_status] => 2
)
[3] => Array
(
[history_id] => 41
[sla_group_id] => 0
[sla_id] => -1
[create_time] => 1513069534
[status] => 2
[tasklog_id] => 41
[tasklog_time] => 2017-12-12 17:05:34
[tasklog_name] => centos7-11自增非整数测试
[tasklog_user] => admin
[tasklog_type] => 立即调度
[tasklog_status] => 2
)
[4] => Array
(
[history_id] => 40
[sla_group_id] => 0
[sla_id] => -1
[create_time] => 1513067574
[status] => 2
[tasklog_id] => 40
[tasklog_time] => 2017-12-12 16:32:54
[tasklog_name] => win2008安装MySQL
[tasklog_user] => impp.cc
[tasklog_type] => 策略备份
[tasklog_status] => 2
)
[5] => Array
(
[history_id] => 39
[sla_group_id] => 0
[sla_id] => -1
[create_time] => 1513067399
[status] => 2
[tasklog_id] => 39
[tasklog_time] => 2017-12-12 16:29:59
[tasklog_name] => Linux下文件
[tasklog_user] => ppstorm.com
[tasklog_type] => 立即调度
[tasklog_status] => 2
)
)
二、二维数组排序、搜索
// HTTP GET values
$length = (int) $this->input->get('length', true);
$start = (int) $this->input->get('start', true);
$order = trim($this->input->get('order', true));
$sort = trim($this->input->get('dir', true));
$search = trim($this->input->get('search', true));
// 三表数据
$vm_group = $this->safe->vm_group_task();
$vm_tasklog = $this->safe->vm_tasklog();
$vm_mounts_log = $this->safe->vm_mounts_log();
// 合并数据
$tasklog = array_merge($vm_group, $vm_tasklog, $vm_mounts_log);
// !!! 二维数据搜索 !!!
if (!empty($search)) {
foreach ($tasklog as $sk => $sv) {
if (mb_stripos($sv['tasklog_name'], $search) === false
&& mb_stripos($sv['tasklog_type'], $search) === false
&& mb_stripos($sv['tasklog_user'], $search) === false
) {
unset($tasklog[$sk]);
continue;
}
}
}
// 总记录
$total_record = count($tasklog);
// 排序
switch ($order) {
case 1:
$sort_key = 'tasklog_name';
break;
case 2:
$sort_key = 'tasklog_type';
break;
case 3:
$sort_key = 'tasklog_user';
break;
case 4:
$sort_key = 'tasklog_time';
break;
case 5:
$sort_key = 'tasklog_status';
break;
default:
$sort_key = 'tasklog_id';
break;
}
$sort_arr = array_column($tasklog, $sort_key);
$desc_asc = $sort === 'desc' ? SORT_DESC : SORT_ASC;
array_multisort($sort_arr, $desc_asc, $tasklog); // !!! 二维数据排序 !!!
// 取指定长度
$output_arr = array_slice($tasklog, $start, $length);