in_array无法处理动态填充的数组

时间:2018-05-01 14:27:12

标签: php sql arrays sql-server

// Listing of holidays, Down Fridays, etc... 
$sqlHol = "SELECT holidayDate FROM holidays";
$holidays = array(); 
$stmtHol = sqlsrv_query( $conn, $sqlHol );

if( $stmtHol === false ) 
{
     die( print_r( sqlsrv_errors(), true));
}               
if ( $stmtHol )
{ 
    $rowsHol = sqlsrv_has_rows( $stmtHol );
    if ($rowsHol === false)
    {
         die( print_r( sqlsrv_errors(), true));
    }
    else while( $rowsHol = sqlsrv_fetch_array( $stmtHol, SQLSRV_FETCH_ASSOC) )
    {
        $holidays[] = $rowsHol['holidayDate']; // This is where I fill my array
    }
}       

var_dump $ holidays //这是数组$ holidays的var_dump
数组([0] => 2018-01-01 1 => 2018-01-12 [2] => 2018-01-15 [3] => 2018-01-26 [4 ] => 2018-02-09 [5] => 2018-02-19 [6] => 2018-02-23 [7] => 2018-03-09 [8] => 2018- 03-23 [9] => 2018-04-06 [10] => 2018-04-20 [11] => 2018-05-11 [12] => 2018-05-25 [13] => 2018-05-28 [14] => 2018-06-01 [15] => 2018-06-15 [16] => 2018-06-29 [17] => 2018-07 -04 [18] => 2018-07-13 [19] => 2018-07-27 [20] => 2018-08-10 [21] => 2018-08-24 [22] = > 2018-09-03 [23] => 2018-09-07 [24] => 2018-09-21 [25] => 2018-10-05 [26] => 2018-10- 08 [27] => 2018-10-19 [28] => 2018-11-02 [29] => 2018-11-12 [30] => 2018-11-16 [31] => ; 2018-11-22 [32] => 2018-11-30 [33] => 2018-12-14 [34] => 2018-12-25 [35] => 2018-12-28 [36] => 2019-01-01 [37] => 2019-01-11 [38] => 2019-01-21 [39] => 2019-01-25 [40] => 2019-02-08 [41] => 2019-02-18 [42] => 2019-02-22 [43] => 2019-03-08 [44] => 2019-03-22 [ 45] => 2019-04-05 [46] => 2019-04-19 [47] => 2019-05-03 [48] => 2019-05-17 [49] => 2019-05-27 [50] => 2019-05-31 [51] => 2019-06-14 [52] => 2019-06-28 [53] => 2019-07-04 [54] => 2019-07-12 [55] => 2019-07-26 [56] => 2019-08-09 [57] => 2019-08-23 [58] => 2019-09-02 [59] => 2019-09-06 [60] => 2019-09-20 [61] => 2019-10-04 [62] => 2019-10-14 [63] => 2019-10-18 [64] => 2019-11-01 [65] => 2019-11-11 [66] => 2019-11-15 [67] => 2019-11-28 [68] => 2019-11-29 [69] => 2019-12-13 [70] => 2019-12-25 [71] => 2019-12-27 [72] => 2020-01-01 [73] => 2020-01-10 [74] => 2020-01-20 [75] => 2020-01-24 [76] => 2020-02-07 [77] => 2020-02-17 [78] => 2020-02-21 [79] => 2020-03-06 [80] => 2020-03-20 [81] => 2020-04-03 [82] => 2020-04-17 [83] => 2020-05-01 [84] => 2020-05-15 [85] => 2020-05-25 [86] => 2020-05-29 [87] => 2020-06-12 [88] => 2020-06-26 [89] => 2020-07-03 [90] => 2020-07-10 [91] => 2020-07-24 [92] => 2020-08-07 [93] => 2020-08-21 [94] => 2020-09-04 [95] => 2020-09-07 [96] => 2020-09-18 [97] => 2020-10-02 [98] => 2020-10-12 [99] => 2020-10-16 [100] => 2020-10-30 [101] => 2020-11-11 [102] => 2020-11-13 [103] => 2020-11-26 [104] => 2020-11-27 [105] => 2020-12-11 [106] => 2020-12-25 [107] => 2020-12-25)

但是,在将$ next1WD与$ holidays进行比较时,count5WD中使用的in_array找不到数组中任何值的匹配项。一个例子是学生从18-18年开始,应该在18-27-2018完成。它需要排除周末和周五(18-2018)。结果应该是从开始的7天,但我得到8天,因为周五没有跳过(找到)。

// Calculate the number of days between start and end dates
$count5WD = 0;
$temp = strtotime( $rowsBlk['startDate'] ); //example as today is 2016-03-25
$curHrs = 59.5/9;  //Total course hours/hours per day = ~7 days
while( $count5WD<$curHrs )
{
    $next1WD = strtotime( '+1 weekday', $temp );
    $next1WDDate = date( 'Y-m-d', $next1WD );
    if( in_array( $next1WDDate, $holidays ) ) //looks for $next1WD in the array
    {
        $count5WD++;
    }
    $temp = $next1WD;
}

$next5WD = date( "m/d/Y H:i:s", $temp ); //this is the outputted future date

var_dump $ next1WDDate //这是数组$ next1WDDate 的var_dump
2018-04-192018-04-202018-04-232018-04-242018-04-252018-04-262018-04-272018-04-302018-05-012018-05-022018-05-032018-05-042018- 05-072018-05-082018-05-092018-05-102018-05-112018-05-142018-05-152018-05-162018-05-172018-05-182018-05-212018-05-222018-05- 232018-05-242018-05-252018-05-282018-05-292018-05-302018-05-312018-06-012018-06-042018-06-052018-06-062018-06-072018-06-082018- 06-112018-06-122018-06-132018-06-142018-06-152018-06-182018-06-192018-06-202018-06-212018-06-222018-06-252018-06-262018-06- 272018-06-28

静态加载数组(见下文),程序运行正常。我认为它与数组中的数据格式有关,但我无法弄明白。比较数组数据和$ next1WD数据似乎表明它们是相同的。

$holidays = array(
    '2018-01-01',
    '2018-01-12',
    '2018-01-15',
    '2018-01-26',
    '2018-02-09',
    '2018-02-19',
    '2018-02-23',
    '2018-03-09',
    '2018-03-23',
    '2018-04-06',
    '2018-04-20',
    '2018-05-04',
    '2018-05-18',
    '2018-05-28',
    '2018-06-01',
    '2018-06-15',
    '2018-06-29',
);

0 个答案:

没有答案