3

抱歉,如果问题的标题不清楚,我无法更准确地总结。

这是问题:

首先,我有一个这种格式的数组:

Array ( 
    [0] => 09:00 
    [1] => 10:00 
    [2] => 11:00 
    [3] => 12:00 
    [4] => 13:00 
    [5] => 14:00 
    [6] => 15:00 
    [7] => 16:00 
    [8] => 17:00 
    [9] => 18:00 
) 

然后一些成员是未设置的,所以之后我们留下了类似的东西:

Array ( 
    [0] => 09:00 
    [1] => 10:00 
    [6] => 15:00 
    [7] => 16:00 
    [8] => 17:00 
    [9] => 18:00 
) 

如您所见,该数组表示时隙。现在,我需要做的是消除所有少于 3 小时的时间段。所以我需要遍历数组,如果原始数组的成员少于 3 个,也将它们取出。所以在上面的例子中,由于 09:00 和 10:00 之后没有 11:00,我需要将它们取出并留下:

Array ( 
    [6] => 15:00 
    [7] => 16:00 
    [8] => 17:00 
    [9] => 18:00 
)  

我该如何做到这一点?从逻辑上讲,我认为检查 3 个连续索引可能最简单,而不是检查实际时间,但我愿意接受任何建议。

4

4 回答 4

1

我自己解决了这个问题,并且我把它变成了通用的,所以它可以在任何时间内工作,而不仅仅是 3 小时。

$dur=3;  //could be anything
foreach($work_times as $member){
    $key=array_search($member,$work_times);
    $a_ok=0;
    for($options=0;$options<$dur;$options++){
        $thisone=1;
        for($try=$key-$options;$try<$key-$options+$dur;$try++){
            if(!array_key_exists($try,$work_times))
                $thisone=0;
        }
        if($thisone==1)
            $a_ok=1;
    }
    if($a_ok==0)
        unset($work_times[$key]);
}
于 2012-04-26T11:18:44.543 回答
0
$a = Array(
    0 => "09:00",
    1 => "10:00",
    6 => "15:00",
    7 => "16:00",
    8 => "17:00",
    9 => "18:00",
    11 => "20:00",
);

foreach ($a as $k => $v) {
    // previous or next two time slots exist
    $consecutive = (isset($a[$k-1]) or
                    (isset($a[$k+1]) and isset($a[$k+2])));
    if (!$consecutive)
        unset($a[$k]);
}
于 2012-04-25T10:10:19.850 回答
0
$arr = array(
  0 => '09:00',
  1 => '10:00',
  6 => '15:00',
  7 => '16:00',
  8 => '17:00',
  9 => '18:00'
);
// for some testing
$arr += array(12 => '19:00', 13 => '20:00', 14 => '21:00');
$arr += array(16 => '22:00', 17 => '23:00');


$dontRemove = array();

foreach($arr as $key => $val) {
  // if the next 2 keys are set
  if (isset($arr[$key+1]) && isset($arr[$key+2])) {
    $dontRemove[] = $key;
    $dontRemove[] = $key+1;
    $dontRemove[] = $key+2;
  }
}

// combine and diff the keys to get the keys which should be actually removed
$remove = array_diff(array_keys($arr), array_unique($dontRemove));

foreach($remove as $key) {
  unset($arr[$key]);
}

print_r($arr);
于 2012-04-25T10:15:46.697 回答
0

试试这个:

<?php
function check() {
    global $array;

    $tmpArr = array_keys( $array );

    $val1 = $tmpArr[0];
    $val2 = $tmpArr[1];
    $val3 = $tmpArr[2];

    if( ( ++$val1 == $val2 ) && ( ++$val2 == $val3 ) ) {
        // continuous
    } else {
        // not continuous, remove it
        unset( $array[$tmpArr[0]] );
    }
}

$array = array( 
    '0' => '09:00', 
    '1'=> '10:00', 
    '6' => '15:00',
    '7'=> '16:00',
    '8' => '17:00',
    '9' => '18:00'
);

$total = count( $array );
$ctotal = 0;
while( $ctotal < $total ) {
    if( count( $array ) <= 2 ) {
        // this array has 2 elements left, which obviously
        // nullifies the 3 continuous element check
        $array = array();
        break;
    } else {
        //check the array backwards
        check();
        $total--;
    }
}
?>

希望这可以帮助

于 2012-04-25T10:35:15.043 回答