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

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

问题描述

我有这样的数组:

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

任何想法?

推荐答案

这应该为你工作:

所以基本上你刚才通过你的整个数组循环,并检查下一个元素仍设置和当前的数组是一样的下一个(这意味着它们具有相同的小时)。如果是这样做,直到while循环返回false。这是本code:

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项,你从一个到一天创建的范围。这是本code:

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天相同的时间,你只需将它添加到结果数组。这是本code:

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]];

和按多少天同一时间你跳到下一个数组元素。这是本code:

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

$dayCount += ($DayAmountOfConsecutiveSameHours - 1);

全部code:

<?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天全站免登陆