2012-07-20 57 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