Hi there,
The following works from what I have tried, but it is fairly long. As this task has interested me I will look into a quicker, neater and easier way of doing it (which could well be more obvious than I realise, but it’s late at the moment!). Anyway, until I have a better way (or anyone else does for that matter), I will post what I have at the moment:
[php] $avail_times = array(“9”,“11”,“12”,“13”,“14”,“16”,“17”,“18”,“19”,“20”,“21”,“22”);
$groups = array(0 => array($avail_times[0])); //Put first time into first group array
$i = 0;
foreach($avail_times as $key => $time)
{
if($key != 0) //Ignore the first time, as it is already used
{
$last_time = !isset($groups[$i]) ? $time-1 : $groups[$i][count($groups[$i])-1]; //Get last set time
if($last_time + 1 == $time)
{
$groups[$i][] = $time; //Add this number to the end of a group
}
else
{
$i++; //Increment group count
$groups[$i][] = $time; //Create a new group with this time as the first one
}
}
}
foreach($groups as $key => $times)
{
if(count($times) < 4)
{
unset($groups[$key]); //Unset any groups of less than 4
}
}
sort($groups); //Reset top-level keys (group ids)
echo "<pre>";
print_r($groups); //Have a gander at what's left!
echo "</pre>";
/*
This example looks like:
Array
(
[0] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
)
[1] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
)
)
*/[/php]