Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Same basic principle as in Zuker's response but instead of relying on a continous array using a "previous state" variable.</p> <pre><code>&lt;?php $result = array(); // * $end = '00:00:00'; foreach( getData() as $d ) { if ( $d['start_time']!=$end ) { $result[] = array('name'=&gt;'available', 'start_time'=&gt;$end, 'finish_time'=&gt;$d['start_time'] ); } $end = $d['finish_time']; $result[] = $d; } // * $result[] = array('name'=&gt;'available', 'start_time'=&gt;$end, 'finish_time'=&gt;'23:59:59' ); print_r($result); function getData() { return array( array( 'name'=&gt;'event A', 'start_time'=&gt;'01:45:00', 'finish_time'=&gt;'02:30:15' ), array( 'name'=&gt;'event B', 'start_time'=&gt;'02:30:15', 'finish_time'=&gt;'03:00:00' ), array( 'name'=&gt;'event C', 'start_time'=&gt;'03:15:00', 'finish_time'=&gt;'06:15:00' ), ); } </code></pre> <p>prints</p> <pre><code>Array ( [0] =&gt; Array ( [name] =&gt; available [start_time] =&gt; 00:00:00 [finish_time] =&gt; 01:45:00 ) [1] =&gt; Array ( [name] =&gt; event A [start_time] =&gt; 01:45:00 [finish_time] =&gt; 02:30:15 ) [2] =&gt; Array ( [name] =&gt; event B [start_time] =&gt; 02:30:15 [finish_time] =&gt; 03:00:00 ) [3] =&gt; Array ( [name] =&gt; available [start_time] =&gt; 03:00:00 [finish_time] =&gt; 03:15:00 ) [4] =&gt; Array ( [name] =&gt; event C [start_time] =&gt; 03:15:00 [finish_time] =&gt; 06:15:00 ) [5] =&gt; Array ( [name] =&gt; available [start_time] =&gt; 06:15:00 [finish_time] =&gt; 23:59:59 ) ) </code></pre> <p>(*) this does not cover events that begin on the previous day and end on the current day or begin on the current day and end on the next day. For brevity's sake I left out the date part, but those "edge cases" should be trivial.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload