如何找到从第一次出现到最后一次出现的数组变量范围?

时间:2014-12-19 03:42:02

标签: php

问题:

一个人从当天的第一次约会开始直到他们最后一次约会为止 - 中间的非预订时间包括在内。

示例数组如下所示:

array(
    [0] => stdClass Object ( [booking] => )
    [1] => stdClass Object ( [booking] => )
    [2] => stdClass Object ( [booking] => 2014-12-08 09:00:00 )
    [3] => stdClass Object ( [booking] => )
    [4] => stdClass Object ( [booking] => )
    [5] => stdClass Object ( [booking] => 2014-12-08 10:30:00 )
    [6] => stdClass Object ( [booking] => )
    [7] => stdClass Object ( [booking] => )
    [8] => stdClass Object ( [booking] => )
)

期望的输出:

array(
    [2] => stdClass Object ( [booking] => 2014-12-08 09:00:00 )
    [3] => stdClass Object ( [booking] => )
    [4] => stdClass Object ( [booking] => )
    [5] => stdClass Object ( [booking] => 2014-12-08 10:30:00 )
)

计数= 4

如何提取所需的数据集?

1 个答案:

答案 0 :(得分:0)

过早提出我认为的问题并且可能有更有效的方法来执行此操作但是通过下面的两个循环运行数组会将前导和训练空白条目删除到它们各自的第一次出现:

for($i=0; $i < count($timesheet); $i++){
    if ($timesheet[$i]['booking'] == 0) {
        unset($timesheet[$i]);
    } else {
        break;
    }
}

$timesheet = array_values($timesheet);

for($i=count($timesheet)-1; $i > 0; $i--){
    if ($timesheet[$i]['booking'] == 0) {
        unset($timesheet[$i]);
    } else {
        break;
    }
}