如何筛选日期时间范围数组以避免日期时间重叠



我有一个json对象

$json='[{"from":"2019-07-22 09:00:00","to":"2019-07-22 11:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 08:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 08:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 20:00:00","to":"2019-07-22 22:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 10:00:00","to":"2019-07-22 17:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 20:00:00","to":"2019-07-22 22:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 15:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"}]';

数组格式

Array
(
    [0] => Array
        (
            [from] => 2019-07-22 09:00:00
            [to] => 2019-07-22 11:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
    [1] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 08:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
    [2] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 08:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Open
        )
    [3] => Array
        (
            [from] => 2019-07-22 20:00:00
            [to] => 2019-07-22 22:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Open
        )
    [4] => Array
        (
            [from] => 2019-07-22 10:00:00
            [to] => 2019-07-22 17:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Open
        )
    [5] => Array
        (
            [from] => 2019-07-22 20:00:00
            [to] => 2019-07-22 22:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
    [6] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 15:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
)

我需要对这个数组进行排序,这样就不会有两个日期冲突在一起,并且不时地总是不同的。以下是我需要对这个数组进行排序的格式。

Array
(
    [0] => Array
        (
            [from] => 2019-07-22 09:00:00
            [to] => 2019-07-22 11:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
    [1] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 08:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Close
        )
    [2] => Array
        (
            [from] => 2019-07-22 20:00:00
            [to] => 2019-07-22 22:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Open
        )
    [3] => Array
        (
            [from] => 2019-07-22 11:00:00
            [to] => 2019-07-22 17:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => Open
        )
)

此数组的排序方式是,它排除了介于任意两个日期之间的任何日期时间,并且还开始下一个开始日期时间,其中不会干扰数组的另一个索引。

到目前为止,我已经尝试了以下代码

$newSlotselse=array();
                            foreach($thisthearray as $closekey=>$closeval){
                                if(empty($newSlotselse)){
                                    $newSlotselse[]=$closeval;
                                }else{
                                    $curfrom=$closeval['from'];
                                    $curto=$closeval['to'];
                                    foreach($newSlotselse as $exceptionkey=>$exceptionval){
                                        //some static vars
                                        $consider=true;
                                        $case=0;
                                        if($curfrom==$exceptionval['from'] && $curto==$exceptionval['to']){
                                            //case 1
                                            $consider=false;
                                            $case=1;
                                            break;
                                        }else if($curfrom < $exceptionval['to'] && $curto > $exceptionval['to']){
                                            //case 2
                                            $curfrom=$exceptionval['to'];
                                            $case=2;
                                            break;
                                        }else if(($curfrom < $exceptionval['from'] && $curto < $exceptionval['to']) && ($curto > $exceptionval['from'])){
                                            //case 3
                                            $curto=$exceptionval['from'];
                                            $case=3;
                                            break;
                                        }else if($curfrom > $exceptionval['from'] && $curto < $exceptionval['to']){
                                            //case 4
                                            $curfrom=$exceptionval['to'];
                                            $case=4;
                                            break;
                                        }else if($curfrom < $exceptionval['from'] && $curto > $exceptionval['to']){
                                            //case 5
                                            $consider=false;
                                            $case=5;
                                            break;
                                        }else if($curfrom > $exceptionval['from'] && $curto > $exceptionval['to']){
                                            //case 6
                                            $consider=true;
                                            $case=6;
                                            break;
                                        }else if($curfrom < $exceptionval['from'] && $curto < $exceptionval['to']){
                                            //case 7
                                            $consider=true;
                                            $case=7;
                                            break;
                                        }
                                    }

                                    if($consider){
                                        $newSlotselse[]=array('from'=>$curfrom, 'to'=>$curto, 'rendering'=>$closeval['rendering'], 'resource'=>$closedval['resource'], 'backgroundColor'=>$closedval['backgroundColor'], 'purpose'=>$closeval['purpose'], 'case'=>$case, 'iteration'=>$closekey);
                                    }
                                }
                            }

我的结果是

<pre>Array
(
    [0] => Array
        (
            [from] => 2019-07-22 09:00:00
            [to] => 2019-07-22 11:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [1] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 08:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [2] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 08:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [3] => Array
        (
            [from] => 2019-07-22 20:00:00
            [to] => 2019-07-22 22:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [4] => Array
        (
            [from] => 2019-07-22 10:00:00
            [to] => 2019-07-22 17:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [5] => Array
        (
            [from] => 2019-07-22 20:00:00
            [to] => 2019-07-22 22:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
    [6] => Array
        (
            [from] => 2019-07-22 07:00:00
            [to] => 2019-07-22 15:00:00
            [rendering] => background
            [resource] => 3529
            [backgroundColor] => #660000
            [purpose] => closedexception
        )
)

这不是我需要的。

在构建输出数组时,它会充当不断增长的查找数组,以筛选出不完全可用的新条目。

代码:(演示(

$json='[{"from":"2019-07-22 09:00:00","to":"2019-07-22 11:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 08:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 08:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 20:00:00","to":"2019-07-22 22:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 11:00:00","to":"2019-07-22 17:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Open"},{"from":"2019-07-22 20:00:00","to":"2019-07-22 22:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"},{"from":"2019-07-22 07:00:00","to":"2019-07-22 15:00:00","rendering":"background","resource":"3529","backgroundColor":"#660000","purpose":"Close"}]';
//var_export(json_decode($json, true));
foreach (json_decode($json, true) as $i => $entry) {
    if (!$i) {
        $result[] = $entry;
    } else {
        foreach ($result as $stored) {
            if (($entry['from'] >= $stored['from'] && $entry['from'] < $stored['to'])
                || ($entry['to'] > $stored['from'] && $entry['to'] <= $stored['to'])) {
                continue 2;
            }
        }
        $result[] = $entry;
    }
}
var_export($result);

输出:(我必须在 json 输入中将10更改为11才能产生此内容。

array (
  0 => 
  array (
    'from' => '2019-07-22 09:00:00',
    'to' => '2019-07-22 11:00:00',
    'rendering' => 'background',
    'resource' => '3529',
    'backgroundColor' => '#660000',
    'purpose' => 'Close',
  ),
  1 => 
  array (
    'from' => '2019-07-22 07:00:00',
    'to' => '2019-07-22 08:00:00',
    'rendering' => 'background',
    'resource' => '3529',
    'backgroundColor' => '#660000',
    'purpose' => 'Close',
  ),
  2 => 
  array (
    'from' => '2019-07-22 20:00:00',
    'to' => '2019-07-22 22:00:00',
    'rendering' => 'background',
    'resource' => '3529',
    'backgroundColor' => '#660000',
    'purpose' => 'Open',
  ),
  3 => 
  array (
    'from' => '2019-07-22 11:00:00',
    'to' => '2019-07-22 17:00:00',
    'rendering' => 'background',
    'resource' => '3529',
    'backgroundColor' => '#660000',
    'purpose' => 'Open',
  ),
)
  • 无论如何,第一个条目都会被推入结果中。
  • continue 2意味着发生了日期时间冲突,没有理由再迭代内部循环。

最新更新