PHP completes the missing date data in the query interval date

PHP completes the missing date data in the query interval date



foreword

This article is the situation I encountered when I was writing the project, and I hereby record it so that I can use it for reference next time I encounter it.


1. Functional requirements

The function that needs to be implemented this time is to cooperate with the front-end to realize the histogram and bar chart, and to process the missing data in the interval date required by the histogram.


2. Functions and methods used

1. PHP built-in function array_column()
definition: array_column() returns the value of a single column in the input array.
Usage: array_column(array,column,index);//array refers to the array to be processed, column refers to the column that needs to be taken out, these two are required, index is the column of the index/key of the returned array, optional.

<?php
$data = [
	  [
	  	"order_num"=> 1,
	   	"today_pay"=>"10.00", 
	   	"dateymd"=>'2022-8-1'
	   ],
	   [
	  	"order_num"=> 2,
	   	"today_pay"=>"20.00", 
	   	"dateymd"=>'2022-8-2'
	   ],
];
$dateymd = array_column($data, 'dateymd');//
print_r($dateymd);
?>

Output: [
0 => '2022-8-1',
1 => '2022-8-2'
]

2. PHP built-in function array_multisort()
definition: array_multisort() returns an ordered array.
How to use: array_multisort(array, sorting order, sorting type) //array refers to the array to be processed, you can add an array, the array will be sorted in order, the sorting order specifies the sorting order, and the sorting type specifies the sorting type.

<?php
$arr=[11,3,5,17,22];
array_multisort($arr,SORT_ASC,SORT_NUMERIC);
print_r($arr);
?>

Output:
[
0 => 3,
1 => 5,
2 => 11,
3 => 17,
4 => 22
]

3. CompletionDate method
The date of the completion time period

//补全日期
function CompletionDate($from, $to)
{
    
    	
	//定义一个空数组
    $date = [];
    //将字符串转换为时间戳
    $from = strtotime($from);
    $to = strtotime($to);
    while ($from <= $to) {
    
    
    	//将时间装进$date数组
        $date[] += $from;
        $from = strtotime('+1 day', $from);
    }
    //&的使用可以改变原来元素的值,这里也就是将$date里的时间戳全部转换为字符串的时间
    foreach ($date as &$value) {
    
    
        $value = date('Y-m-d', $value);
    }
    return $date;
}

4.duplicate method,
the array is deduplicated according to the key

//按键去重
function duplicate($list,$key)
{
    
    
	//定义两个空数组
    $res = [];
    $keys = [];
    foreach($list as $item){
    
    
    	//in_array(string,array) 判断string是否在array中
        if(!in_array($item[$key],$keys)){
    
    
        	//array_push(array,string) 将string添加至array的最后
            array_push($res,$item);
            array_push($keys,$item[$key]);
        }
    }
    return $res;
}

3. Code implementation

<?php
$alldate = CompletionDate($from,$to);//间隔所有日期
$time = [];
foreach ($data as $v) {
    
    //数据库有的
	$time = array_merge($time,[$v['dateymd']]);
}
foreach ($alldate as $all){
    
    
	if (!in_array($all,$time)){
    
    //将数据库查出没有的日期插入$data中
		array_push($data,["order_num"=> 0, "today_pay"=>"0.00", "dateymd"=>$all]);
	}
}
$dateymd = array_column($data,'dateymd');
array_multisort($dateymd,SORT_ASC,$data);
$data = duplicate($data,'dateymd');
?>

Summarize

The above is all the codes I used to implement the functions, and I also learned from many other articles. The implementation method may not be the best, but it is also a way to solve the problem.
Reference: PHP array sorted by a value
Reference: PHP completes the missing date in the date range

Guess you like

Origin blog.csdn.net/SupremeT_T/article/details/126420068
Recommended