来自Hour Array PHP的时间块

时间:2012-10-11 12:01:25

标签: php arrays multidimensional-array

希望有人可以提供帮助,因为我正在绞尽脑汁想要弄明白。

我有一个新系统,我正在创建一个用户可用且不可用的时间,我有两个阵列:

可用的:

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
        )

)

每个阵列都有小时位,例如。上午12点至凌晨1点,凌晨1点至凌晨2点等。

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

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

有人可以建议任何解决方案吗?

感谢。

-EDIT-只是为了澄清我的追求: 这是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] =>一个         )

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

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

2 个答案:

答案 0 :(得分: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作为键的数组,并将“availability”字符串格式化为值。当然应该添加一些智能检查和字符串修改以避免使用逗号。

答案 1 :(得分:0)

据我所知..你想把阵列分成两组

  • A:可用
  • U:不可用

您可以使用数组过滤器

$avialable =  array_filter($array,function($var) { return $var[2] == "A" ; });
$unavialabe =  array_filter($array,function($var) { return $var[2] == "U" ; });