Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $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);
- 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',
- ),
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement