0

希望有人可以提供帮助,因为我正在绞尽脑汁试图弄清楚。

我有一个我正在创建的新系统,它可以在用户可用和不可用的时间结束,我有两个数组:

可用的:

Array
(
    [0] => Array
        (
            [0] => 00:00:00
            [1] => 01:00:00
        )

    [1] => Array
        (
            [0] => 01:00:00
            [1] => 02:00:00
        )

    [2] => Array
        (
            [0] => 03:00:00
            [1] => 04:00:00
        )

    [3] => Array
        (
            [0] => 04:00:00
            [1] => 05:00:00
        )

)

不可用:

Array
(
    [0] => Array
        (
            [0] => 02:00:00
            [1] => 03:00:00
        )

    [1] => Array
        (
            [0] => 05:00:00
            [1] => 06:00:00
        )

    [2] => Array
        (
            [0] => 06:00:00
            [1] => 07:00:00
        )

    [3] => Array
        (
            [0] => 07:00:00
            [1] => 08:00:00
        )

    [4] => Array
        (
            [0] => 08:00:00
            [1] => 09:00:00
        )

    [5] => Array
        (
            [0] => 09:00:00
            [1] => 10:00:00
        )

    [6] => Array
        (
            [0] => 10:00:00
            [1] => 11:00:00
        )

    [7] => Array
        (
            [0] => 11:00:00
            [1] => 12:00:00
        )

    [8] => Array
        (
            [0] => 12:00:00
            [1] => 13:00:00
        )

    [9] => Array
        (
            [0] => 13:00:00
            [1] => 14:00:00
        )

    [10] => Array
        (
            [0] => 14:00:00
            [1] => 15:00:00
        )

    [11] => Array
        (
            [0] => 15:00:00
            [1] => 16:00:00
        )

    [12] => Array
        (
            [0] => 16:00:00
            [1] => 17:00:00
        )

    [13] => Array
        (
            [0] => 17:00:00
            [1] => 18:00:00
        )

    [14] => Array
        (
            [0] => 18:00:00
            [1] => 19:00:00
        )

    [15] => Array
        (
            [0] => 19:00:00
            [1] => 20:00:00
        )

    [16] => Array
        (
            [0] => 20:00:00
            [1] => 21:00:00
        )

    [17] => Array
        (
            [0] => 21:00:00
            [1] => 22:00:00
        )

    [18] => Array
        (
            [0] => 22:00:00
            [1] => 23:00:00
        )

    [19] => Array
        (
            [0] => 23:00:00
            [1] => 00:00:00
        )

)

每个数组都有小时槽,例如。12am-1am, 1am-2am 等

但我正试图找到一种方法来分组时间,例如。

可用:00:00:00 - 02:00:00 和 03:00:00 - 05:00:00 和不可用:05:00:00 - 00:00:00

任何人都可以提出任何解决方案吗?

谢谢。

-编辑- 只是为了澄清我所追求的:这是 1 人 1 天。如果他们把它们放在下午 6 点到 10 点之间可用,它将以小时的形式传递给我。18:00:00 - 19:00:00、19:00:00 - 20:00:00 等。但我只需要 18:00:00 - 22:00:00。谢谢。

我也可以使用这个数组:

Array

([0] => 数组([0] => 00:00:00 [1] => 01:00:00 [2] => A)

[1] => Array
    (
        [0] => 01:00:00
        [1] => 02:00:00
        [2] => A
    )

[2] => Array
    (
        [0] => 02:00:00
        [1] => 03:00:00
        [2] => U
    )

[3] => Array
    (
        [0] => 03:00:00
        [1] => 04:00:00
        [2] => A
    )

[4] => Array
    (
        [0] => 04:00:00
        [1] => 05:00:00
        [2] => A
    )

[5] => Array
    (
        [0] => 05:00:00
        [1] => 06:00:00
        [2] => U
    )

[6] => Array
    (
        [0] => 06:00:00
        [1] => 07:00:00
        [2] => U
    )

[7] => Array
    (
        [0] => 07:00:00
        [1] => 08:00:00
        [2] => U
    )

[8] => Array
    (
        [0] => 08:00:00
        [1] => 09:00:00
        [2] => U
    )

[9] => Array
    (
        [0] => 09:00:00
        [1] => 10:00:00
        [2] => U
    )

[10] => Array
    (
        [0] => 10:00:00
        [1] => 11:00:00
        [2] => U
    )

[11] => Array
    (
        [0] => 11:00:00
        [1] => 12:00:00
        [2] => U
    )

[12] => Array
    (
        [0] => 12:00:00
        [1] => 13:00:00
        [2] => U
    )

[13] => Array
    (
        [0] => 13:00:00
        [1] => 14:00:00
        [2] => U
    )

[14] => Array
    (
        [0] => 14:00:00
        [1] => 15:00:00
        [2] => U
    )

[15] => Array
    (
        [0] => 15:00:00
        [1] => 16:00:00
        [2] => U
    )

[16] => Array
    (
        [0] => 16:00:00
        [1] => 17:00:00
        [2] => U
    )

[17] => Array
    (
        [0] => 17:00:00
        [1] => 18:00:00
        [2] => U
    )

[18] => Array
    (
        [0] => 18:00:00
        [1] => 19:00:00
        [2] => U
    )

[19] => Array
    (
        [0] => 19:00:00
        [1] => 20:00:00
        [2] => U
    )

[20] => Array
    (
        [0] => 20:00:00
        [1] => 21:00:00
        [2] => U
    )

[21] => Array
    (
        [0] => 21:00:00
        [1] => 22:00:00
        [2] => U
    )

[22] => Array
    (
        [0] => 22:00:00
        [1] => 23:00:00
        [2] => U
    )

[23] => Array
    (
        [0] => 23:00:00
        [1] => 00:00:00
        [2] => U
    )

)

但我只需要将它们分组。谢谢。

4

2 回答 2

0

我猜你想得到一个格式化的字符串输出。

您必须遍历数组键并检查两个数组。假设所有用户都存在于数组中$available$unavailable您可以尝试这样的事情:

$userSlots = array();

foreach(array_keys($available) as $userKey) {

    $userSlots[ $userKey ] = 'Available: ';

    foreach ($available[ $userKey ] as $slot) {
       $userSlots[ $userKey ] .= $slot[ 0 ] . ' - ' . $slot[ 1 ] . ', ';
    }

    $aggregatedSlots .= 'Unavailable: ';

    foreach ($unavailable[ $userKey ] as $slot) {
       $userSlots[ $userKey ] .= $slot[ 0 ] . ' - ' . $slot[ 1 ] . ', ';
    }
}

您将获得一个数组,其中 userKeys 作为键,格式化的“可用性”字符串作为值。当然,应该添加一些智能检查和字符串修改以避免尾随逗号。

于 2012-10-11T12:19:37.000 回答
0

据我了解..您想将数组分成两组

  • 答:可用
  • U:不可用

你可以只使用数组过滤器

$avialable =  array_filter($array,function($var) { return $var[2] == "A" ; });
$unavialabe =  array_filter($array,function($var) { return $var[2] == "U" ; });
于 2012-10-11T14:43:20.117 回答