2012-07-20 59 views
1

我正在構建基於「圖層」的每週日曆應用程序。這個日曆是基於日時矩陣在$日曆變量表示爲數組:內部條件的倍數循環

hour |Monday | Tuesday | Wednesday ... 
12am 
01am 
02am 
... 

如果想申請假期日曆我做的:

$holidays = getHolidays(); 
for($day = 0; $i < count($calendar)); $day ++) 
{ 
    for($hour = 0; $i < count($calendar[$day])); $hour ++) 
    { 
     if (exists_in_array($calendar[$day][$hour] , $holidays)) 
     { 
      $calendar[$day][$hour] = "holiday"; 
     } 
    } 
} 

現在,如果我想申請設置特殊的事件中,我做的事:

$specialDates = getSpecialDates(); 

for($day = 0; $i < count($calendar)); $day ++) 
{ 
    for($hour = 0; $i < count($calendar[$day])); $hour ++) 
    { 
     if (exists_in_array($calendar[$day][$hour] , $specialDates )) 
     { 
      $calendar[$day][$hour] = "special"; 
     } 
    } 
} 

在這個時刻,我很擔心,因爲有一個循環來遍歷日曆,以申請一個新層,可以使應用程序更慢,速度慢。

因此,在我的日曆中添加不同的信息是否是一種好的做法(在我的情況下)?

回答

2

爲什麼不使用同一組循環來代替多次循環?

$specialDates = getSpecialDates(); 
$holidays = getHolidays(); 
for($day = 0; $i < count($calendar)); $day ++) 
{ 
    for($hour = 0; $i < count($calendar[$day])); $hour ++) 
    { 
     if (exists_in_array($calendar[$day][$hour] , $specialDates )) 
     { 
      $calendar[$day][$hour] = "special"; 
     } 
     if (exists_in_array($calendar[$day][$hour] , $holidays)) 
     { 
      $calendar[$day][$hour] = "holiday"; 
     } 
    } 
} 
+0

嗯,我的問題是面向循環數和條件。我的意思是,假設我有100層和50個條件。我應該只有一個內部有60個條件的循環,還是保持100個內部有一個條件的loos? – manix 2012-07-21 20:47:52

+1

就我個人而言,如果我要循環遍歷相同的信息,我嘗試只運行一次循環。另外只是一個供參考,你不應該在你的for循環像count($ calendar [$ day])中放置一個操作,你應該真的移動它,否則它會重新計算每次它通過循環。 – Pitchinnate 2012-07-24 17:07:08

+0

非常感謝你!還有其他的pleople。 – manix 2012-07-25 18:07:41