结合营业时间相似的日子

编程入门 行业动态 更新时间:2024-10-26 06:29:43
本文介绍了结合营业时间相似的日子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何编写PHP中的函数(使用CodeIgniter)将具有相似开放营业时间的天数合并在一起。例如,如果我们有:

How might I code a function in PHP (with CodeIgniter) to merge days with similar opening hours of a store together. For example, if we have:

Mon 9am-5pm Tue 9am-5pm Wed 9am-5pm Thu 9am-5pm Fri 9am-5pm Sat 9am-7pm Sun 9am-7pm

我想让代码简化为:

Mon-Fri 9am-5pm Sat-Sun 9am-7pm

if / else或case ifs?我正在使用CodeIgniter ..

How do I do this without a long list of if/else or case ifs? I'm using CodeIgniter..

推荐答案

<?php $openHours = array( 'Mon' => '9am-5pm', 'Tue' => '9am-5pm', 'Wed' => '9am-9pm', 'Thu' => '9am-5pm', 'Fri' => '9am-5pm', 'Sat' => '9am-7pm', 'Sun' => '9am-7pm' ); $summaries = array(); foreach ($openHours as $day => $hours) { if (count($summaries) === 0) { $current = false; } else { $current = &$summaries[count($summaries) - 1]; } if ($current === false || $current['hours'] !== $hours) { $summaries[] = array('hours' => $hours, 'days' => array($day)); } else { $current['days'][] = $day; } } foreach ($summaries as $summary) { if (count($summary['days']) === 1) { echo reset($summary['days']) . ' ' . $summary['hours'] . PHP_EOL; } else { echo reset($summary['days']) . '-' . end($summary['days']) . ' ' . $summary['hours'] . PHP_EOL; } }

键盘示例

更多推荐

结合营业时间相似的日子

本文发布于:2023-10-23 01:42:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:营业时间   日子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!