php - 如何从具有相同星期几的数组中删除日期

I want to remove date from an array by having same day of week from another array.

This was i try:

$intervalDates = ['2017-01-23','2017-01-24','2017-01-25','2017-01-26','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-02-03','2017-02-04','2017-02-05','2017-02-06','2017-02-07','2017-02-08','2017-02-09','2017-02-10'];

$tmpWeek = ['2016-10-28','2016-10-29','2016-10-30'];  



for($i = 0; $i < count($intervalDates); $i++){

    for($j = 0; $j < count($tmpWeek); $j++){

         if($intervalDates[$i]->dayOfWeek == $tmpWeek[$j]->dayOfWeek){
              unset($intervalDates[$i]);

              $intervalDates = array_values($intervalDates);


         }
   }
}

but I got the error undefined offset.

Please help.

First create day names array using $tmpWeek, then remove duplicate values from $dayNames. Then loop $intervalDates and remove same days and create new array.

$intervalDates = array('2016-10-28','2016-10-29','2016-10-30','2017-01-23','2017-01-24','2017-01-25','2017-01-26','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-02-03','2017-02-04','2017-02-05','2017-02-06','2017-02-07','2017-02-08','2017-02-09','2017-02-10');

$tmpWeek = array('2016-10-28','2016-10-29','2016-10-30');

// create day names array
$dayNames = array();
foreach($tmpWeek as $day){
    $dayNames[] = date('l', strtotime($day));
}
//  remove duplicate date names
$dayNames = array_unique($dayNames);

$data =array();
foreach($intervalDates as $date){
    if(!in_array(date('l', strtotime($date)), $dayNames)){
            $data[] = $date;
    }
}  

echo "<pre>";
print_r($data);
echo "</pre>";

You should debug your code by printing something inside if statement code. See for what value of $i or $j it is returning undefined offset. The error simply means that, you are using index for the array which is not defined on that array.

What happened?

You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.

It looked for your key in the array, and found undefined.

Check this:

$intervalDates = ['2017-01-23', '2017-01-24', '2017-01-25', '2017-01-26', '2017-01-27', '2017-01-28', '2017-01-29', '2017-01-30', '2017-01-31', '2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04', '2017-02-05', '2017-02-06', '2017-02-07', '2017-02-08', '2017-02-09', '2017-02-10'];

$tmpWeek = ['2016-10-28', '2016-10-29', '2016-10-30'];



for ($i = 0; $i < count($intervalDates); $i++) {

    for ($j = 0; $j < count($tmpWeek); $j++) {
        if (array_key_exists($i, $intervalDates) && array_key_exists($j, $tmpWeek)) {
            $dayofweek_1 = date('w', strtotime($intervalDates[$i]));

            $dayofweek_2 = date('w', strtotime($tmpWeek[$j]));


            if ($dayofweek_1 == $dayofweek_2) {
                unset($intervalDates[$i]);

                $intervalDates = array_values($intervalDates);
            }
        }
    }
}

Update (If you want to remove all first dates):

for ($j = 0; $j < count($tmpWeek); $j++) {
        if (array_key_exists($i, $intervalDates) && array_key_exists($j, $tmpWeek)) {

            $dayofweek_1 = date('w', strtotime($intervalDates[$i]));
            $dayofweek_firstday = date('d', strtotime($intervalDates[$i]));
            $dayofweek_2 = date('w', strtotime($tmpWeek[$j]));


            if ($dayofweek_1 == $dayofweek_2 || $dayofweek_firstday == 1) {
                unset($intervalDates[$i]);

                $intervalDates = array_values($intervalDates);
            }
        }
    }