检查PHP数组是否具有连续索引

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个连续的索引可能比检查实际时间更容易,但我对任何建议都持开放态度。


1
但是,你有15-16-17-18,却没有19,所以,17-18也会被移除! - hjpotter92
那只是一个例子,显然那不是我想要使用的逻辑(删除任何两个成员后面没有第三个成员的情况)。我需要以某种方式检查每个元素是否有两个前面的元素、两个后面的元素或者一边各一个元素 - 只有在找不到这三个选项中的任何一个时才删除它。 - sveti petar
1
然后,您需要解释您想要使用什么逻辑。我们只能猜测。 - dan-lee
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]);
}

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

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

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

希望这能有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接