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.
Let’s say I start with this:
$query = array(
[0] =>
array(
[0] =>
array(
['name'] =>'A'
['start'] =>'1/1/2011'
['end'] =>'1/31/2011'
)
[1] =>
array(
['name'] =>'B'
['start'] =>'1/15/2011'
['end'] =>'1/31/2011'
)
[2] =>
array(
['name'] =>'C'
['start'] =>'2/1/2011'
['end'] =>'2/28/2011'
)
[3] =>
array(
['name'] =>'D'
['start'] =>'2/2/2011'
['end'] =>'2/28/2011'
)
[4] =>
array(
['name'] =>'E'
['start'] =>'1/31/2011'
['end'] =>'3/1/2011'
)
[5] =>
array(
['name'] =>'F'
['start'] =>'3/3/2011'
['end'] =>'3/31/2011'
)
)
)
And I want to finish with this:
$result = array(
[0] =>
array(
[0] =>
array(
['name'] =>'A'
['start'] =>'1/1/2011'
['end'] =>'1/31/2011'
)
[1] =>
array(
['name'] =>'C'
['start'] =>'2/1/2011'
['end'] =>'2/28/2011'
)
[2] =>
array(
['name'] =>'F'
['start'] =>'3/3/2011'
['end'] =>'3/31/2011'
)
)
[1]=>
array(
[0] =>
array(
['name'] =>'B'
['start'] =>'1/15/2011'
['end'] =>'1/31/2011'
)
[1] =>
array(
['name'] =>'D'
['start'] =>'2/2/2011'
['end'] =>'2/28/2011'
)
)
[2]=>
array(
[0] =>
array(
['name'] =>'E'
['start'] =>'1/31/2011'
['end'] =>'3/1/2011'
)
)
)
edit: by request, the var_export for the input and output listed above:
$query = array ( 0 => array ( 'name' => 'A', 'start' => '1/1/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'B', 'start' => '1/15/2011', 'end' => '1/31/2011', ), 2 => array ( 'name' => 'C', 'start' => '2/1/2011', 'end' => '2/28/2011', ), 3 => array ( 'name' => 'D', 'start' => '2/2/2011', 'end' => '2/28/2011', ), 4 => array ( 'name' => 'E', 'start' => '1/31/2011', 'end' => '3/1/2011', ), 5 => array ( 'name' => 'F', 'start' => '3/3/2011', 'end' => '3/31/2011', ), )
$result = array ( 0 => array ( 0 => array ( 'name' => 'A', 'start' => '1/1/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'C', 'start' => '2/1/2011', 'end' => '2/28/2011', ), 2 => array ( 'name' => 'F', 'start' => '3/3/2011', 'end' => '3/31/2011', ), ), 1 => array ( 0 => array ( 'name' => 'B', 'start' => '1/15/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'D', 'start' => '2/2/2011', 'end' => '2/28/2011', ), ), 2 => array ( 0 => array ( 'name' => 'E', 'start' => '1/31/2011', 'end' => '3/1/2011', ), ), )
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.)
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.
I have what I believe to be the start of the answer here.
I’m looping through the $query and using unset() on each object as it’s added to arrays within $result.
Hopefully what’s written here is as efficient as possible:
And here’s the output of this: