2016-10-01 10 views
1

私はキーと値の開始日と終了日を持つ配列を持っています。例えば、 。PHPで日付範囲を結合するアルゴリズム

/* formate 'mm/dd/yyyy'=> 'mm/dd/yyyy'*/ 

$arr = array(
'01/01/2016'=>'01/01/2016', 
'01/02/2016'=>'01/02/2016', 
'01/03/2016'=>'01/03/2016', 


'04/10/2016'=>'04/10/2016', 
'04/11/2016'=>'04/11/2016', 
'04/12/2016'=>'04/12/2016', 


'04/25/2016'=>'04/25/2016', 

'04/30/2016'=>'04/30/2016', 
'05/01/2016'=>'05/01/2016', 
'05/02/2016'=>'05/02/2016' } 

) 

ここでは、いくつかの要素が連続していることがわかります。例えば、 。最初の3つの要素には04/01から04/03の日付があります。私は1つの要素でそれを望みます。新しい配列はこのようになります>

$arr = array(
    '01/01/2016'=>'01/03/2016', 

    '04/10/2016'=>'04/12/2016', 

    '04/25/2016'=>'04/25/2016', 

    '04/30/2016'=>'05/02/2016' 
}) 

どうすればいいですか?

おかげ

+0

日付が昇順であるか、ないのだろうか? –

+0

はい、月/日/年になるようにしてください。 BTWメインアレイは既に適切な順序であります。 –

答えて

2

私は答えはすでにあります知っているが、ここで私のバージョンだが - そうしたかったことに少しの時間を過ごしましたシェア!

私の例では、元の配列が日付順であり、キーと値が常に同じであると仮定しています。

次のようにあなたがあなたの元のデータセットを反復処理し、グループ化された日付の配列を返す関数を使用することができます

...

function group_dates($dates_array) { 

    // prepare results array 
    $result = array(); 

    // take the first date from the submitted array 
    $group_start = array_shift($dates_array); 
    $timestamp_previous_iteration = strtotime($group_start); 

    // iterate over remaining values 
    foreach ($dates_array as $date) { 
     // calculate current iteration's timestamp 
     $timestamp_current = strtotime($date); 

     // calculate timestamp for 1 day before the current value 
     $timestamp_yesterday = strtotime('-1 day', $timestamp_current); 

     if ($timestamp_previous_iteration != $timestamp_yesterday) { 
     // create group... 
     $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration); 
     // ...and store the next group's start date 
     $group_start = $date; 
     } 

     // log timestamp for next iteration 
     $timestamp_previous_iteration = $timestamp_current; 
    } 

    // add final group 
    $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration); 

    return $result; 
} 

次のように、その関数を使用することができ、

$result_array = group_dates($arr); 

あなたの例で配列に関数を与えると、あなたが要求した配列になります。

日付がMM/DD/YYYYの形式であるため、文字列はstrtotime()でUNIXのタイムスタンプに正しく変換されます。他の日付書式を使用していた場合は、PHPのDateTimeオブジェクトを調べることをお勧めします。

リファレンス
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/book.datetime.php

+0

良いです。ありがとうございました –

1

は、以下のコード

<?php 

    $arr = array(
     '01/01/2016'=>'01/01/2016', 
     '01/02/2016'=>'01/02/2016', 
     '01/03/2016'=>'01/03/2016', 
     '04/10/2016'=>'04/10/2016', 
     '04/11/2016'=>'04/11/2016', 
     '04/12/2016'=>'04/12/2016', 
     '04/25/2016'=>'04/25/2016', 
     '04/30/2016'=>'04/30/2016', 
     '05/01/2016'=>'05/01/2016', 
     '05/02/2016'=>'05/02/2016' 
    ); 

    $lastDate = null; 
    $ranges = array(); 
    $currentRange = array(); 

    foreach ($arr as $date => $value) { 
     $temp = $value; 
     $value = date_create(date('m/d/Y', strtotime($value))); 

     if (null === $lastDate) { 
      $currentRange[] = $temp; 
     } else { 
      // get the Date object 
      $interval = date_diff($value, $lastDate); 

      if ($interval->days === 1) { 
       // add this date to the current range 
       $currentRange[] = $temp;  
      } else { 
       // store the old range and start a new 
       $ranges[] = $currentRange; 
       $currentRange = array($temp); 
      } 
     } 
     $lastDate = $value; 
    } 

    $ranges[] = $currentRange; 

    $datemerge = array(); 
    foreach ($ranges as $key) { 
     $count = count($key); 
     $datemerge[$key[0]] = $key[$count-1]; 
    } 

    print_r($datemerge); 

?> 

出力チェック:

Array 
(
    [01/01/2016] => 01/03/2016 
    [04/10/2016] => 04/12/2016 
    [04/25/2016] => 04/25/2016 
    [04/30/2016] => 05/02/2016 
) 
+0

良いありがとうございます –

関連する問題