duanlv5084
2016-10-29 21:30
浏览 33
已采纳

基于12小时定制时钟的排序数组

My array is as follows:

Array ( [01:00 pm] => Lunch [03:00 pm] => Dance [12:45 pm] => Guests arrive [12:50 pm] => Find seats [12:55 pm] => Blessing )

I need to convert these keys to a customized time format for sorting so that a correct order would look like this:

Array ( [12:45 pm] => Guests arrive [12:50 pm] => Find seats [12:55 pm] => Blessing [01:00 pm] => Lunch  [03:00 pm] => Dance )

But that's not all. These parties often end at 1am or 2am in the morning. Therefore it would make sense to start the clock with 3am being the first value. As such, a 02:00 am entry would appear at the end of the sorted list and a 05:00 am entry would appear at the very beginning (because I want to designate 03:00 am as the beginning of the clock.

If I had a 02:00 am and 05:00 am example in the array, it would read as:

Array ( [05:00 am] => Wake up [12:45 pm] => Guests arrive [12:50 pm] => Find seats [12:55 pm] => Blessing [01:00 pm] => Lunch  [03:00 pm] => Dance [02:00 am] => Party ends)

I don't know where or how to transform the keys from the array. I currently have:

 ksort($event_order);
                foreach ( $event_order as $time => $event) {
                    echo '<div><strong>';
                    echo $time;
                    echo '</strong> - ';
                    echo $event;
                    echo '</div>';
                }

图片转代码服务由CSDN问答提供 功能建议

我的数组如下:

  Array([01  :00 pm] =&gt;午餐[03:00 pm] =&gt;跳舞[12:45 pm] =&gt;客人到达[12:50 pm] =&gt;查找座位[12:55 pm] =&gt;祝福 )
   
 
 

我需要将这些键转换为自定义时间格式进行排序,以便正确的顺序如下所示: < pre> Array([12:45 pm] =&gt;客人到达[12:50 pm] =&gt;查找座位[12:55 pm] =&gt;祝福[01:00 pm] =&gt;午餐[ 03:00 pm] =&gt;舞蹈)

但并非全部。 这些派对通常在凌晨1点或凌晨2点结束。 因此,以3am为第一个值启动时钟是有意义的。 因此,02:00 am条目将出现在排序列表的末尾,并且最开始的05:00 am条目将出现在开头(因为我想将03:00 am指定为时钟的开头。

如果我在数组中有02:00 am和05:00 am示例,它将显示为:

  Array([05  :00 am] =&gt;醒来[下午12:45] =&gt;客人到达[12:50 pm] =&gt;查找座位[12:55 pm] =&gt; Blessing [01:00 pm] =&gt; 午餐[03:00 pm] =&gt;舞蹈[02:00 am] =&gt;派对结束)
   
 
 

我不知道在哪里或如何改造 数组中的键。我目前有:

  ksort($ event_order); 
 foreach($ event_order as $ time =&gt; $ event){
 echo'  &lt; div&gt;&lt; strong&gt;'; 
 echo $ time; 
 echo'&lt; / strong&gt;  - '; 
 echo $ event; 
 echo'&lt; / div&gt;'; 
} 
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandai0373 2016-10-29 22:03
    已采纳

    Here is one way of doing it, using array_multisort, mapping the times strings to number of seconds since midnight, and moving the first 3 hours to the end:

    $event_order = Array ( 
        "12:45 pm" => "Guests arrive",
        "12:55 pm" => "Blessing",
        "02:00 am" => "Party ends",
        "05:00 am" => "Wake up",
        "01:00 pm" => "Lunch",
        "03:00 pm" => "Dance",
        "12:50 pm" => "Find seats");
    
    array_multisort(array_map(function ($v) {
        return (strtotime($v) - strtotime("midnight") + 21*3600) % (24*3600);
    }, array_keys($event_order)), $event_order);
    
    print_r($event_order);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题