如何根据依赖于PHP中的值的索引拆分数组?

Basically I am trying to generate a warning when someone tries to book more than 10 consecutive holiday days and I have managed to get an array that has END as an element after each set (when the consecutive days end), but I don't know how to split the array up so that I would have access to the date ranges for each time there were 10 or more consecutive days being booked off. Here is the current code:

/* == CURRENT INPUT === */ 

$bankhols[]="2016-05-30";
$bankhols[]="2016-08-29";
$bankhols[]="2016-12-26";
$bankhols[]="2016-12-27";

$bankhols[]="2017-01-02";
$bankhols[]="2017-04-14";
$bankhols[]="2017-04-17";
$bankhols[]="2017-05-01";
$bankhols[]="2017-05-29";
$bankhols[]="2017-08-28";
$bankhols[]="2017-12-25";
$bankhols[]="2017-12-26";

$ten_days_check = array(
    '2016-07-16',
    '2016-07-17',
    '2016-07-18',
    '2016-07-19',
    '2016-07-20',
    '2016-07-21',
    '2016-07-22',
    '2016-07-23',
    '2016-07-24',
    '2016-07-25',
    '2016-07-26',
    '2016-07-27',
    '2016-07-28',
    '2016-07-29',
    '2016-07-30',
    '2016-07-31',
    '2016-08-01',
    '2016-08-02',
    '2016-08-03',
    '2016-08-04',
    '2016-08-05',
    '2016-08-06',
    '2016-08-07',
    '2016-08-08',
    '2016-08-09',
    '2016-08-10',
    '2016-08-11',
    '2016-08-12',
    //'2016-08-13',
    '2016-08-14',
    '2016-08-15',
    '2016-08-16',
    '2016-08-17',
    '2016-08-18',
    '2016-08-19',
    '2016-08-20',
    '2016-08-21',
    '2016-08-22',
    '2016-08-23',
    '2016-08-24',
    '2016-08-25',
    '2016-08-26',
    '2016-08-27',
    '2016-08-28',
    '2016-08-29',
    '2016-08-30',
);

/* === ENDINPUT === */ 


$counter = 0;

foreach($ten_days_check as $date) {
    $datetime = strtotime($date);

    $nextday = date('Y-m-d', strtotime($date . ' + 1 day'));


    if(date('N', $datetime) != 6 && date('N', $datetime) != 7 && !in_array($date, $bankhols)) {
        $counter++;
        $set_of_ten[] = $date;
    }

    if(!in_array($nextday, $ten_days_check)) {
        if($date != $ten_days_check[count($ten_days_check) - 1]) {
            //$set_of_ten[] = $counter;
            $set_of_ten[] = "END";
            $counter = 0;
        } else {
            //$set_of_ten[] = $counter;
            $set_of_ten[] = "END";
        }
    }
}

I want to flag up the fact that between the dates 2016-07-16 and 2016-07-26 is over 10 consecutive days, and continue doing that with all dates that are over 10 consecutive days (excluding of course bank holidays and weekend).

I didn't quite understand your problem, but if you are trying to group the dates array into an array of 10 each, then maybe you are looking for array_chunk.

$days_group = array_chunk($ten_days_check , 10);

This will distribute the array into a multidimensional array of arrays with 10 elements each.

On a side note If I were to implement the functionality to "generate a warning when someone tries to book more than 10 consecutive holiday days " in my own code, I'd do as follows.

  1. Catch and store the dates the user has made the bookings on in an array say $bookings.
  2. Each time the user submits the booking I add the date in my $bookings array.
  3. Run array_unique on $bookings to filter out duplicate bookings.
  4. Count the number of bookings in the array.
  5. If the count is grater than 10, it means we need to apply the warning logic.
  6. Check if there are 10 consecutive dates in the array using simple php logic.

    1. Sort the array in asc.
    2. Iterate over the array and maintain a counter, for the consequtive dates found.
    3. Reset it if a non-consecutive date is found.
    4. If the count > 10 Warning!!!!!
  7. If found, Generate Warning.

  8. ....
  9. Profit!!!
<?php

function has_ten_consecutive_days($dates) {
    sort($dates);
    $last = null;
    $consec = 0;
    foreach($dates as $date) {
        $ts = strtotime($date);
        if($last) {
            $between = $ts - $last;
            if($between == 24 * 60 * 60) {
                $consec++;
                if($consec == 10)
                    return true;
            } else {
                $consec = 0;
            }
        }
        $last = $ts;
    }

    return false;
}

Create some dates to test the above:

function timestamps_to_dateish($timestamps) {
    return array_map(
        function($item) {
            return date('Y-m-d', $item);
        },
        $timestamps
    );
}

// Create some sample data
$first_ts = strtotime('2016-07-27');
$timestamps = array(); 

// Build a list of 13 consecutive days in timestamps
foreach(range(0,12) as $num) {
    $timestamps[] = $first_ts + $num * 24 * 60 * 60;
}

$ts1 = $timestamps;
$ts2 = $timestamps;
$ts2[6] += 7 * 24 * 60 * 60; // Replace the 7th day with a day a week after

$dates1 = timestamps_to_dateish($ts1);
$dates2 = timestamps_to_dateish($ts2);

var_dump($dates1);
var_dump(has_ten_consecutive_days($dates1));

var_dump($dates2);
var_dump(has_ten_consecutive_days($dates2));

Output:

array (size=13)
  0 => string '2016-07-27' (length=10)
  1 => string '2016-07-28' (length=10)
  2 => string '2016-07-29' (length=10)
  3 => string '2016-07-30' (length=10)
  4 => string '2016-07-31' (length=10)
  5 => string '2016-08-01' (length=10)
  6 => string '2016-08-02' (length=10)
  7 => string '2016-08-03' (length=10)
  8 => string '2016-08-04' (length=10)
  9 => string '2016-08-05' (length=10)
  10 => string '2016-08-06' (length=10)
  11 => string '2016-08-07' (length=10)
  12 => string '2016-08-08' (length=10)
boolean true
array (size=13)
  0 => string '2016-07-27' (length=10)
  1 => string '2016-07-28' (length=10)
  2 => string '2016-07-29' (length=10)
  3 => string '2016-07-30' (length=10)
  4 => string '2016-07-31' (length=10)
  5 => string '2016-08-01' (length=10)
  6 => string '2016-08-09' (length=10)
  7 => string '2016-08-03' (length=10)
  8 => string '2016-08-04' (length=10)
  9 => string '2016-08-05' (length=10)
  10 => string '2016-08-06' (length=10)
  11 => string '2016-08-07' (length=10)
  12 => string '2016-08-08' (length=10)
boolean false