2013-07-11 85 views
1

我有一個腳本,我需要通過(星期一到星期四)和(星期五到星期日)分開幾個月。PHP foreach沒有得到數組的最後一個值

我正在使用函數來創建一個範圍內的所有日期的數組,然後通過該數組循環來分隔日子。

function createDateRangeArray($strDateFrom,$strDateTo){ 
    $aryRange=array(); 

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),  substr($strDateFrom,8,2),substr($strDateFrom,0,4)); 
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),  substr($strDateTo,8,2),substr($strDateTo,0,4)); 

    if ($iDateTo>=$iDateFrom) 
    { 
     array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry 
     while ($iDateFrom<$iDateTo) 
     { 
      $iDateFrom+=86400; // add 24 hours 
      array_push($aryRange,date('Y-m-d',$iDateFrom)); 
     } 
    } 
    return $aryRange; 
} 

$date = "2013-07-01"; 
$end_date = "2013-07-31"; 
$date_range = createDateRangeArray($date, $end_date); 

$final_array = array(); 
$temp_array = array(); 

foreach ($date_range as $value) { 
    $temp_array[] = $value; 
    $day_of_week = date("N", strtotime($value)); 

    if ($day_of_week == 4 || $day_of_week == 7) { 
     $final_array[] = $temp_array; 
     $temp_array = array(); 
    } 

} 

當我做的print_r($ DATE_RANGE)我得到:

Array 
(
    [0] => 2013-07-01 
    [1] => 2013-07-02 
    [2] => 2013-07-03 
    [3] => 2013-07-04 
    [4] => 2013-07-05 
    [5] => 2013-07-06 
    [6] => 2013-07-07 
    [7] => 2013-07-08 
    [8] => 2013-07-09 
    [9] => 2013-07-10 
    [10] => 2013-07-11 
    [11] => 2013-07-12 
    [12] => 2013-07-13 
    [13] => 2013-07-14 
    [14] => 2013-07-15 
    [15] => 2013-07-16 
    [16] => 2013-07-17 
    [17] => 2013-07-18 
    [18] => 2013-07-19 
    [19] => 2013-07-20 
    [20] => 2013-07-21 
    [21] => 2013-07-22 
    [22] => 2013-07-23 
    [23] => 2013-07-24 
    [24] => 2013-07-25 
    [25] => 2013-07-26 
    [26] => 2013-07-27 
    [27] => 2013-07-28 
    [28] => 2013-07-29 
    [29] => 2013-07-30 
    [30] => 2013-07-31 
) 

然而,當我運行它通過foreach循環它上面搶奪之外的所有月份的最後幾天。如果我跑的print_r($ final_array)我得到這樣的:

Array 
(
    [0] => Array 
     (
      [0] => 2013-07-01 
      [1] => 2013-07-02 
      [2] => 2013-07-03 
      [3] => 2013-07-04 
     ) 

    [1] => Array 
     (
      [0] => 2013-07-05 
      [1] => 2013-07-06 
      [2] => 2013-07-07 
     ) 

    [2] => Array 
     (
      [0] => 2013-07-08 
      [1] => 2013-07-09 
      [2] => 2013-07-10 
      [3] => 2013-07-11 
     ) 

    [3] => Array 
     (
      [0] => 2013-07-12 
      [1] => 2013-07-13 
      [2] => 2013-07-14 
     ) 

    [4] => Array 
     (
      [0] => 2013-07-15 
      [1] => 2013-07-16 
      [2] => 2013-07-17 
      [3] => 2013-07-18 
     ) 

    [5] => Array 
     (
      [0] => 2013-07-19 
      [1] => 2013-07-20 
      [2] => 2013-07-21 
     ) 

    [6] => Array 
     (
      [0] => 2013-07-22 
      [1] => 2013-07-23 
      [2] => 2013-07-24 
      [3] => 2013-07-25 
     ) 

    [7] => Array 
     (
      [0] => 2013-07-26 
      [1] => 2013-07-27 
      [2] => 2013-07-28 
     ) 

) 

什麼是更加古怪的是,當我更改日期範圍內(2013年6月1日至2013-06-30)或(2013- 08-01到2013-08-31)它將所有日期按照它應該的方式分成$ final_array。

回答

2

我的思考過程是你錯過了加入$temp_array的最後一步,因爲你不是很瞭解47標記。它適用於其他日期,因爲星期幾設置得很好。

嘗試通過在foreach後加入陣列(如果不爲空)確認:

foreach ($date_range as $value) { 
    $temp_array[] = $value; 
    $day_of_week = date("N", strtotime($value)); 

    if ($day_of_week == 4 || $day_of_week == 7) { 
     $final_array[] = $temp_array; 
     $temp_array = array(); 
    } 

} 

if (count($temp_array) > 0){ // anything leftover? 
    $final_array[] = $temp_array; // append to $final_array 
} 
+0

哈哈,我不知道我怎麼錯過了。它工作完美。只要它能讓我接受,我就會將其標記爲已接受的答案。 –

+0

有時只需要另一雙眼睛;)高興地幫忙! –

相關問題