检查PHP数组中的连续索引

时间:2012-04-25 09:40:20

标签: php arrays

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

这就是问题所在:

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

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 个答案:

答案 0 :(得分: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]);
}

答案 1 :(得分: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]);
}

答案 2 :(得分: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);

答案 3 :(得分: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--;
    }
}
?>

希望这有帮助