PHP:在多维数组上有条件地操作和删除重复元素

时间:2018-06-06 06:46:46

标签: php arrays

我有以下多维数组。我没有得到适合我的问题的标题。您可以自由编辑和更改。

Array(
    [2018-05-01] => Array
        (
            [0] => Array
                (
                    [Zone] => 0
                    [Time] => 13:06
                )

            [1] => Array
                (
                    [Zone] => 0
                    [Time] => 13:08
                )

            [2] => Array
                (
                    [Zone] => 2
                    [Time] => 13:10
                )

            [3] => Array
                (
                    [Zone] => 2
                    [Time] => 13:14
                )

            [4] => Array
                (
                    [Zone] => 0
                    [Time] => 13:16
                )

            [5] => Array
                (
                    [Zone] => 0
                    [Time] => 13:17
                )

            [6] => Array
                (
                    [Zone] => 2
                    [Time] => 13:21
                )

            [7] => Array
                (
                    [Zone] => 2
                    [Time] => 13:25
                )

            [8] => Array
                (
                    [Zone] => 0
                    [Time] => 13:28
                )

            [9] => Array
                (
                    [Zone] => 0
                    [Time] => 13:32
                )

        )

    [2018-05-04] => Array
        (
            [0] => Array
                (
                    [Zone] => 1
                    [Time] => 13:26
                )

            [1] => Array
                (
                    [Zone] => 1
                    [Time] => 13:28
                )

            [2] => Array
                (
                    [Zone] => 1
                    [Time] => 13:30
                )

            [3] => Array
                (
                    [Zone] => 0
                    [Time] => 13:34
                )

            [4] => Array
                (
                    [Zone] => 0
                    [Time] => 13:36
                )

            [5] => Array
                (
                    [Zone] => 1
                    [Time] => 13:37
                )

            [6] => Array
                (
                    [Zone] => 1
                    [Time] => 13:41
                )

        )
)

我必须以这样一种方式操纵上面的数组,即同一区域不应该同时出现。这取决于条件 -

条件1

如果子数组只有Zone => 0 and Zone => 2。例如,带有键2018-05-01的数组只有0和2作为区域。

我们只会获取First Zone => 0,最后一个Zone => 2,第一区=> 0并且最后一次Zone => 2.

请注意,如果没有Zone => Zone =>之后的2 0,然后在这种情况下我们获取最后的Zone => 0而不是第一个Zone => 0。 请查看我的输出以获得清除。

条件2

如果子数组有Zone => 1。例如,键为2018-05-04的数组。我们只会获取每个的第一次出现。

这是所需的输出

Array(
    [2018-05-01] => Array
        (
            [0] => Array
                (
                    [Zone] => 0
                    [Time] => 13:06
                )

            [3] => Array
                (
                    [Zone] => 2
                    [Time] => 13:14
                )

            [4] => Array
                (
                    [Zone] => 0
                    [Time] => 13:16
                )

            [7] => Array
                (
                    [Zone] => 2
                    [Time] => 13:25
                )

            [9] => Array
                (
                    [Zone] => 0
                    [Time] => 13:32
                )

        )

    [2018-05-04] => Array
        (
            [0] => Array
                (
                    [Zone] => 1
                    [Time] => 13:26
                )


            [3] => Array
                (
                    [Zone] => 0
                    [Time] => 13:34
                )


            [5] => Array
                (
                    [Zone] => 1
                    [Time] => 13:37
                )

        )
)

我尝试的是 -

我一直在考虑的是使用array_column来检查子数组是否有“1”作为Zone值。如果是,那么我们将实现条件1,否则条件2.然后遍历每个子阵列并将当前迭代与前一个迭代进行比较。但我没有得到确切的观点,我怀疑这是否是一个优雅的解决方案。

0 个答案:

没有答案