汇总连续的数组项

时间:2013-11-06 01:00:32

标签: php arrays date

我有一个如下数组:

Array
(
    [10:00-18:00] => Array
        (
            [0] => Mon
            [1] => Tues
            [2] => Wed
            [4] => Fri
            [5] => Sat
        )

    [10:00-21:00] => Array
        (
            [3] => Thu
        )

    [10:00-13:00] => Array
        (
            [6] => Sun
        )

)

在PHP中,我试图总结一下人类会做的日期: 即。减少连续日期:

[10:00-18:00]周一至周三,周五至周六

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

请注意我的输入与您的输入不同...

<?php 
$times = 
      array (
        '10:00-18:00' => 
        array (
          0 => 'Mon',
          1 => 'Tues',
          2 => 'Wed',
          3 => 'Fri',
          4 => 'Sat',
        ),
        '10:00-21:00' => 
        array (
          3 => 'Thu',
        ),
        '11:00-21:00' => 
        array (
          0 => 'Tues',
          1 => 'Wed',
          2 => 'Thu',
        ),
        '10:00-13:00' => 
        array (
          0 => 'Sun',
          1 => 'Mon',
        ),
      );

$info = array(); 
foreach ($times as $time => $days)
{
   $days = process_days($days);  
   $info[] = $time.":  ".implode(', ',$days); 
}

print_r($info); 

/*
Array
(
    [0] => 10:00-18:00:  Mon - Wed, Fri - Sat
    [1] => 10:00-21:00:  
    [2] => 11:00-21:00:  Tue - Thu
    [3] => 10:00-13:00:  Sun - Mon
)
*/


function process_days($days)
{  
   $days = order_the_days($days); 

   $return = array(); 
   $end_day = end($days); 

   reset($days);  

   $consecutive = false;  
   $consecutive_days = array();  
   $current_day = key($days);     

   while ($days[$current_day] !== $end_day)
   { 
      next($days);    
      $next_day = key($days);    

      if ($next_day == $current_day+1 || ($next_day==1 && $current_day=7))
      {
         $consecutive = true;  
         $consecutive_days[] = $current_day;
      }
      else // not consecutive 
      { 
         if ($consecutive===true)
         {  
            $return[] = figure_out_consecutive_days($consecutive_days,$current_day,$days); 
            $consecutive_days = array(); 
         }
         else
         {
            $return[] = $days[$current_day];
         } 
         $consecutive = false; 
      } 

      $current_day = key($days);  
   } 
   if ($consecutive) {
      $return[] = figure_out_consecutive_days($consecutive_days,$current_day,$days);  
   }
   return $return; 
}

function figure_out_consecutive_days($consecutive_days,$current_day,$days)
{
   $consecutive_days[$current_day] = $days[$current_day];
   reset($consecutive_days);
   $first = current($consecutive_days);  
   $end   = end($consecutive_days);  
   return $days[$first]." - $end";  
}

function order_the_days($days)
{
   $return = array(); 
   foreach ($days as $i => $day)
   {
      $day = substr($day,0,3); 
      $dow = date('N', strtotime($day));
      $return[$dow] = $day;
   }
   return $return;
}

?>

答案 1 :(得分:0)

<?php

$times = [ '10:00-18:00' => [  0=>'mon', 1=>'tue', 3=>'thu', 4=>'fri', 5=>'sat'],
        '10:00-21:00' => [ 1=>'tue' , 2=>'wed', 3=> 'thu', 5=> 'sat']];


$info = array( );

foreach ( $times as $time => $days )
{
    $first_day   = false;
    $last_day    = false;
    $info[$time] = array( );
    $day_strings = array( );

    foreach ( $days as $index => $day )
    {
        if ( $last_day + 1 != $index  && $last_day !== false)
        {
            $day_strings[] = $first_day != $last_day ? "$days[$first_day] - $days[$last_day]" : $days[$first_day];
            $first_day     = false;
            $last_day      = false;
        }

        if ( $first_day === false )
            $first_day = $index;

        $last_day = $index;
    }

    if ( $last_day !== false )
        $day_strings[] = $first_day != $last_day ? "$days[$first_day] - $days[$last_day]" : $days[$first_day];

    $info[$time] = implode( ', ', $day_strings );
}
print_r( $info );
?>