2014-10-06 110 views
1

我正在重構日曆應用程序。該日曆具有用戶定義的網格(可以預訂的插槽),但也需要顯示「非網格」預訂。 08:00-09:00時可以考慮「常規」時段,08:39時有人預訂時會出現「不規則」時段。出於商業原因,我需要以不同的方式顯示這些插槽(CSS),否則它們的表現會相同。我已經搜索了PHP手冊,但內置的數組函數並不完全符合我的需要。將二維數組合併到組中

$officialGrid = array(
    array('grid' => TRUE, 'time' => '08:00', 'content' => NULL), 
    array('grid' => TRUE, 'time' => '09:00', 'content' => NULL)); 

$bookings = array(
    array('grid' => NULL, 'time' => '08:00', 'content' => 'Paul Simon'), 
    array('grid' => NULL, 'time' => '08:00', 'content' => 'Art Garfunkel'), 
    array('grid' => NULL, 'time' => '08:39', 'content' => 'Homer J. Simpson')); 

我可以追加這些陣列,但由於性能原因想縮短結果:我也很靈活改變數據類型的值(content可能是一個數組

$timeSlotsToDisplay = array(
    array('grid' => TRUE, 'time' => '08:00', 'content' => 'Paul Simon, Art Garfunkel'), //regular 
    array('grid' => NULL, 'time' => '08:39', 'content' => 'Homer J. Simpson'), //irregular 
    array('grid' => TRUE, 'time' => '09:00', 'content' => NULL)); //vacant 

)。除了開始循環和比較之外,是否有合併這些數組的優雅解決方案?我只需要SELECT DISTINCT ON (time) grid, time, string_agg(content) FROM myDB GROUP BY time ORDER BY time, grid;(請忽略可能的關鍵字,不是由於格式化引用,也沒有測試過查詢)。

+0

不是我所知道的。循環並不邪惡,雖然... – Steve 2014-10-06 23:55:29

+1

最好有時間作爲關鍵(官方網格) – Cheery 2014-10-07 00:32:51

回答

2

我看沒有錯..循環,但我建議爲$officialGrid陣列

$officialGrid = array(
    '08:00' => array('grid' => TRUE, 'content' => NULL), 
    '09:00' => array('grid' => TRUE, 'content' => NULL)); 

$bookings = array(
    array('grid' => NULL, 'time' => '08:00', 'content' => 'Paul Simon'), 
    array('grid' => NULL, 'time' => '08:00', 'content' => 'Art Garfunkel'), 
    array('grid' => NULL, 'time' => '08:39', 'content' => 'Homer J. Simpson')); 

$timeSlotsToDisplay = $officialGrid; 

array_map(function($el) use(&$timeSlotsToDisplay, $officialGrid) { 
    $timeSlotsToDisplay[$el['time']] = array(
    'content' => 
     (isset($timeSlotsToDisplay[$el['time']]) ? 
     trim($timeSlotsToDisplay[$el['time']]['content'], ',') . ',' : '') . 
     $el['content'], 
    'grid' => isset($officialGrid[$el['time']]) ? true : null 
    ); 
}, $bookings); 

ksort($timeSlotsToDisplay); 
var_dump($timeSlotsToDisplay); 

array_map另一種結構可以通過一個單一的foreach環所替代。

0

「我已經搜索了PHP手冊,但是內置的數組函數並不是 正是我所需要的。」

內置陣列功能不僅僅是通過一堆陣列的循環更有效,你應該看看array_mergearray_merge_recursive在PHP對於如何做到這一點,或做它作爲信息的更有效的答案用SQL從數據庫中拉下來。

對於一個實用的php解決方案,如果你可以遍歷每個2d數組中的每一行並獲取你想要的信息,但是這裏假定$ officialGrid中每個數組的鍵的結構與每個數組的鍵相同在$預訂。它應該根據「時間」循環和匹配數據,並跳過預訂中不匹配的數據。取決於你有多少行信息循環可以用於數組,但是它們遠不是最優的。

$officialGrid = Array(
     Array('grid' => TRUE, 'time' => '08:00' // ... arrays assumed to be populated with example data above 

    $bookings = Array(
     Array('grid' => NULL, 'time' => '08:00' // ... arrays assumed to be populated with example data above 

    // fill final array with content from $bookings 
    $timeSlotsToDisplay = $bookings; 


    foreach($officialGrid as $officalkey=>$officalobj) { 
     foreach($timeSlotsToDisplay as $bookkey=>$obj) { 
      if(!isset($officialGrid[$officalobj['time']]) || !isset($timeSlotsToDisplay[$bookobj['time']])) { 
       //one of them is not set.. error 
      } 

       //if the times match when looping 
      if($officialGrid[$officalobj['time']] == $timeSlotsToDisplay[$bookobj['time']]) { 
       //keeps the information from officialgrid data for column, and keep $booking data for content column 
      //the null grid entries will just stay the same, as $bookings oringally had 
       $timeSlotsToDisplay[$officalobj['grid']]; 
       $timeSlotsToDisplay[$bookobj['time']] 
       $timeSlotsToDisplay[$bookobj['content']]; 
      } 
     } 
    } 
+0

@downvoter,謹慎評論? – PeerBr 2014-10-07 01:10:36