2015-07-12 104 views
8

我有這個數組:如何合併具有相同數據的連續子數組?

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

任何想法?

+0

@FrancescoAbeni我已經通過$ OPENING_HOURS陣列試圖環和我檢查數組的下一個元素是否與當前元素相同,但不知何故,這不起作用。 Rizier123完成了我所需要的解釋。 – David

回答

7

這應該爲你工作:通過你的整個陣列

所以基本上你只是循環,並檢查下一個元素仍然設置和當前的數組是一樣的下一個(這意味着它們具有相同的小時)。如果是,則直到while循環返回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]]; 

如果你只有一天的時間相同,你只需將它添加到結果數組。這是什麼代碼:

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

+0

謝謝你的解釋和整個功能。它的確如此工作。在問這個問題之前,我已經開始將數組的下一個元素與當前數組中的元素進行比較,但在這種情況下它並不起作用。再次告訴你。 – David

+0

@大衛不客氣。 – Rizier123

+0

@大衛更新了我的答案,並簡化了一切。希望它更易於理解 – Rizier123

相關問題