Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this array:

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

Any ideas?

like image 221
David Avatar asked Jul 12 '15 16:07

David


People also ask

How do I merge Subarrays?

Consider an array that is divided into two parts and both of the parts are sorted individually. Given the mentioned array and the end index of the first part, merge them to create a sorted array. Update the same array with the merged elements.

What is consecutive Subarray?

For a subarray to contain consecutive integers, The difference between the maximum and minimum element in it should be exactly equal to the subarray's length minus one. All elements in the array should be distinct (we can check this by inserting the elements in a set or using a visited array).


1 Answers

This should work for you:

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

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

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

Full 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);

?>

output:

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

like image 107
Rizier123 Avatar answered Oct 27 '22 13:10

Rizier123