Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the best logic for grouping objects by closest start and end dates?
    primarykey
    data
    text
    <p>I have an array of items that all have start and end dates associated with them. I'd like to group them according to closest start and end dates without any of them overlapping, but I'm having difficulty conjuring what the logic for this should look like.</p> <p>Let's say I start with this:</p> <pre><code>$query = array( [0] =&gt; array( [0] =&gt; array( ['name'] =&gt;'A' ['start'] =&gt;'1/1/2011' ['end'] =&gt;'1/31/2011' ) [1] =&gt; array( ['name'] =&gt;'B' ['start'] =&gt;'1/15/2011' ['end'] =&gt;'1/31/2011' ) [2] =&gt; array( ['name'] =&gt;'C' ['start'] =&gt;'2/1/2011' ['end'] =&gt;'2/28/2011' ) [3] =&gt; array( ['name'] =&gt;'D' ['start'] =&gt;'2/2/2011' ['end'] =&gt;'2/28/2011' ) [4] =&gt; array( ['name'] =&gt;'E' ['start'] =&gt;'1/31/2011' ['end'] =&gt;'3/1/2011' ) [5] =&gt; array( ['name'] =&gt;'F' ['start'] =&gt;'3/3/2011' ['end'] =&gt;'3/31/2011' ) ) ) </code></pre> <p>And I want to finish with this:</p> <pre><code>$result = array( [0] =&gt; array( [0] =&gt; array( ['name'] =&gt;'A' ['start'] =&gt;'1/1/2011' ['end'] =&gt;'1/31/2011' ) [1] =&gt; array( ['name'] =&gt;'C' ['start'] =&gt;'2/1/2011' ['end'] =&gt;'2/28/2011' ) [2] =&gt; array( ['name'] =&gt;'F' ['start'] =&gt;'3/3/2011' ['end'] =&gt;'3/31/2011' ) ) [1]=&gt; array( [0] =&gt; array( ['name'] =&gt;'B' ['start'] =&gt;'1/15/2011' ['end'] =&gt;'1/31/2011' ) [1] =&gt; array( ['name'] =&gt;'D' ['start'] =&gt;'2/2/2011' ['end'] =&gt;'2/28/2011' ) ) [2]=&gt; array( [0] =&gt; array( ['name'] =&gt;'E' ['start'] =&gt;'1/31/2011' ['end'] =&gt;'3/1/2011' ) ) ) </code></pre> <p>edit: by request, the var_export for the input and output listed above:</p> <pre><code>$query = array ( 0 =&gt; array ( 'name' =&gt; 'A', 'start' =&gt; '1/1/2011', 'end' =&gt; '1/31/2011', ), 1 =&gt; array ( 'name' =&gt; 'B', 'start' =&gt; '1/15/2011', 'end' =&gt; '1/31/2011', ), 2 =&gt; array ( 'name' =&gt; 'C', 'start' =&gt; '2/1/2011', 'end' =&gt; '2/28/2011', ), 3 =&gt; array ( 'name' =&gt; 'D', 'start' =&gt; '2/2/2011', 'end' =&gt; '2/28/2011', ), 4 =&gt; array ( 'name' =&gt; 'E', 'start' =&gt; '1/31/2011', 'end' =&gt; '3/1/2011', ), 5 =&gt; array ( 'name' =&gt; 'F', 'start' =&gt; '3/3/2011', 'end' =&gt; '3/31/2011', ), ) $result = array ( 0 =&gt; array ( 0 =&gt; array ( 'name' =&gt; 'A', 'start' =&gt; '1/1/2011', 'end' =&gt; '1/31/2011', ), 1 =&gt; array ( 'name' =&gt; 'C', 'start' =&gt; '2/1/2011', 'end' =&gt; '2/28/2011', ), 2 =&gt; array ( 'name' =&gt; 'F', 'start' =&gt; '3/3/2011', 'end' =&gt; '3/31/2011', ), ), 1 =&gt; array ( 0 =&gt; array ( 'name' =&gt; 'B', 'start' =&gt; '1/15/2011', 'end' =&gt; '1/31/2011', ), 1 =&gt; array ( 'name' =&gt; 'D', 'start' =&gt; '2/2/2011', 'end' =&gt; '2/28/2011', ), ), 2 =&gt; array ( 0 =&gt; array ( 'name' =&gt; 'E', 'start' =&gt; '1/31/2011', 'end' =&gt; '3/1/2011', ), ), ) </code></pre> <p>The best I have so far is to loop through the items in $query, and for each one, compare the start date to the end date of the last item in each of the arrays. Even as I'm typing this out I'm realizing that that would assume we're starting with a $query array that's already in some sort of chronological order (it's random.) </p> <p>We are trying to put a GANTT timeline together with this data, using as few rows as possible to help visualize what our open schedule looks like. It has me stumped. Anyone who can point to the most efficient method of organizing these objects, it'd be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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