2014-10-06 108 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