如何合并其中具有相同数据的连续子数组?

How can I merge consecutive subArrays which have the same data in it?

我有这个数组:

$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'),
);

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

$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 循环 returns false。这是哪个代码:

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

然后,如果您有超过 1 个条目,则创建一个从一天到另一天的范围。这是哪个代码:

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

如果只有 1 天的时间相同,则只需将其添加到结果数组即可。这是哪个代码:

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

并根据您在同一时间有多少天跳过下一个数组元素。这是哪个代码:

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

)

Demo