0

我有一个如下数组:

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] 周一 - 周三,周五 - 周六

有任何想法吗?

4

2 回答 2

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;
}

?>
于 2013-11-06T01:10:07.530 回答
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 );
?>
于 2013-11-06T01:50:59.270 回答