如何合并具有相同数据的连续子数组? [英] How can I merge consecutive subArrays which have the same data in it?

查看:44
本文介绍了如何合并具有相同数据的连续子数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组:

$opening_hours = array(
  'Monday' => array('09:00', '17:00'),
  'Tuesday' => array('09:00', '17:00'),
  'Wednesday' => array('08:00', '13:00'), 
  'Thursday' => array('09:00', '17:00'),
  'Friday' => array('09:00', '17:00'),
  'Saturday' => array('10:00', '16:00'),
  'Sunday' => array('Closed'),
);

我需要以某种方式将这些开放时间合并到应该如下所示的数组中:

I need to somehow merge those opening hours to the array which should look like this:

$merged_opening_hours = array(
  'Monday - Tuesday' => array('09:00', '17:00'),
  'Wednesday' => array('08:00', '13:00'),
  'Thursday - Friday' => array('09:00', '17:00'),
  'Saturday' => array('10:00', '16:00');
  'Sunday' => array('Closed'),
);

有什么想法吗?

推荐答案

这应该适合你:

所以基本上你只需遍历整个数组并检查下一个元素是否仍然设置并且当前数组是否与下一个相同(这意味着它们的时间相同).如果是,则执行此操作直到 w​​hile 循环返回 false.这段代码是什么:

So basically you just loop through your entire array and check if the next element is still set AND the current array is the same as the next one (Which means they have the same hours). If yes you do this until the while loop returns false. Which is this code:

$DayAmountOfConsecutiveSameHours = 1;
while(isset($arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours)]) && 
     ($opening_hours[$arrayKeys[$dayCount]] === $opening_hours[$arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours)]]))
    $DayAmountOfConsecutiveSameHours++;

然后,如果您有 1 个以上的条目,则可以创建一个从一天到另一天的范围.这段代码是什么:

Then if you have more than 1 entry you create a range from one to the other day. Which is this code:

if($DayAmountOfConsecutiveSameHours > 1)
    $result[$arrayKeys[$dayCount] . " - " . $arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours-1)]] = $opening_hours[$arrayKeys[$dayCount]];

如果您只有 1 天的时间相同,则只需将其添加到结果数组中即可.这段代码是什么:

If you only have 1 day with the same hours you just add it to the results array. Which is this code:

else
    $result[$arrayKeys[$dayCount]] = $opening_hours[$arrayKeys[$dayCount]];

并且根据同一小时有多少天你跳过下一个数组元素.这段代码是什么:

And according to how many days the same hours have you skip the next array elements. Which is this code:

$dayCount += ($DayAmountOfConsecutiveSameHours - 1);

完整代码:

<?php

    $opening_hours = [
            "Monday" => ["09:00", "17:00"],
            "Tuesday" => ["09:00", "17:00"],
            "Wednesday" => ["08:00", "13:00"], 
            "Thursday" => ["09:00", "17:00"],
            "Friday" => ["09:00", "17:00"],
            "Saturday" => ["10:00", "16:00"],
            "Sunday" => ["Closed"],
        ];


    $amountOfDays = count($opening_hours);
    $arrayKeys = array_keys($opening_hours);

    for($dayCount = 0; $dayCount < $amountOfDays; $dayCount++) {
        $DayAmountOfConsecutiveSameHours = 1;
        while(isset($arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours)]) && ($opening_hours[$arrayKeys[$dayCount]] === $opening_hours[$arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours)]]))
            $DayAmountOfConsecutiveSameHours++;

        if($DayAmountOfConsecutiveSameHours > 1)
            $result[$arrayKeys[$dayCount] . " - " . $arrayKeys[($dayCount+$DayAmountOfConsecutiveSameHours-1)]] = $opening_hours[$arrayKeys[$dayCount]];
        else
            $result[$arrayKeys[$dayCount]] = $opening_hours[$arrayKeys[$dayCount]];

        $dayCount += ($DayAmountOfConsecutiveSameHours - 1);
    }

    print_r($result);

?>

输出:

Array
(
    [Monday - Tuesday] => Array
        (
            [0] => 09:00
            [1] => 17:00
        )

    [Wednesday] => Array
        (
            [0] => 08:00
            [1] => 13:00
        )

    [Thursday - Friday] => Array
        (
            [0] => 09:00
            [1] => 17:00
        )

    [Saturday] => Array
        (
            [0] => 10:00
            [1] => 16:00
        )

    [Sunday] => Array
        (
            [0] => Closed
        )

)

演示

这篇关于如何合并具有相同数据的连续子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆