我整天有很多不同的时间,它们每隔15分钟上升一次。
Array
(
[0] => 12:00am
[1] => 12:15am
[2] => 12:30am
[3] => 12:45am
[4] => 1:00am
[5] => 1:15am
[6] => 1:30am
[7] => 1:45am
[8] => 2:00am
[9] => 3:15am
[10] => 3:30am
[11] => 3:45am
[12] => 4:00am
[13] => 1:00pm
[14] => 1:15pm
[15] => 1:30pm
);
我想做的是,按顺序将它们分组在一起,然后将它们放入另一个单独的数组中
因此,根据上述数组,我将像这样获得3个新数组
Array
(
[0] => 12:00am
[1] => 12:15am
[2] => 12:30am
[3] => 12:45am
[4] => 1:00am
[5] => 1:15am
[6] => 1:30am
[7] => 1:45am
[8] => 2:00am
);
12:00是主数组的开始,而2:00 am是模式停止的位置,因为下一个是3:15
array
{
[1] => 3:15am
[2] => 3:30am
[3] => 3:45am
[4] => 4:00am
)
3:15 am是主数组中新模式的开始,而4:00 am是模式停止的位置,依此类推。
我一直在尝试遍历主数组,并存储以前的时间以匹配当前的循环+ 15分钟,但是根本无法正常工作,感谢您的任何帮助,谢谢
答案 0 :(得分:1)
谢谢@Wimanicesir
这只是逻辑和增强的代码
<?php
$input = Array
(
"12:00am",
"12:15am",
"12:30am",
"12:45am",
"1:00am",
"1:15am",
"1:30am",
"1:45am",
"2:00am",
"3:15am",
"3:30am",
"3:45am",
"4:00am",
"1:00pm",
"1:15pm",
"1:30pm",
);
$periods = [];
foreach($input as $time) {
$hour = substr($time, 0, strpos($time, ':'));
$partOfDay= substr($time, strlen($time) - 2);
$index = ($hour== "12") ? "0" : floor($hour/2.00001);
$periods[$index.":".$partOfDay][] = $time;
}
echo "<pre>";
var_dump($periods);
echo "</pre>";
exit();
?>
已编辑
按照OP的逻辑,我简化了逻辑,这是代码。
$periods = array();
$index = 0;
$old_value = 0;
foreach($input as $time) {
//converting time to minutes
$new_value = (intval(date("H",strtotime($time)))*60) + intval(date("i",strtotime($time)));
if($new_value > ($old_value+15)){
$index++;
}
$periods[$index][] = $time;
$old_value = $new_value;
}
答案 1 :(得分:1)
代码:
<?php
$times = [
'12:00am',
'12:15am',
'12:30am',
'12:45am',
'1:00am',
'1:15am',
'1:30am',
'1:45am',
'2:00am',
'3:15am',
'3:30am',
'3:45am',
'4:00am',
'1:00pm',
'1:15pm',
'1:30pm'
];
$previous = null;
$results = [[]];
foreach ($times as $time) {
if (
$previous === null ||
(new \DateTime())->createFromFormat('h:ia', $time)->getTimestamp() -
(new \DateTime())->createFromFormat('h:ia', $previous)->getTimestamp() === 15*60
) {
$index = count($results)-1;
} else {
$index = count($results);
}
$results[$index][] = $time;
$previous = $time;
}
echo "<pre>";
var_dump($results);
echo "</pre>";
输出(HTML视图):
array(3) {
[0]=>
array(9) {
[0]=>
string(7) "12:00am"
[1]=>
string(7) "12:15am"
[2]=>
string(7) "12:30am"
[3]=>
string(7) "12:45am"
[4]=>
string(6) "1:00am"
[5]=>
string(6) "1:15am"
[6]=>
string(6) "1:30am"
[7]=>
string(6) "1:45am"
[8]=>
string(6) "2:00am"
}
[1]=>
array(4) {
[0]=>
string(6) "3:15am"
[1]=>
string(6) "3:30am"
[2]=>
string(6) "3:45am"
[3]=>
string(6) "4:00am"
}
[2]=>
array(3) {
[0]=>
string(6) "1:00pm"
[1]=>
string(6) "1:15pm"
[2]=>
string(6) "1:30pm"
}
}
答案 2 :(得分:0)
我制作了一个数组,将时间分为小时和上午/下午。这就是你想要的吗?
<?php
$input =
[
"12:00am", "12:15am", "12:30am", "12:45am",
"1:00am", "1:15am", "1:30am", "1:45am",
"2:00am", "3:15am", "3:30am", "3:45am",
"4:00am", "1:00pm", "1:15pm", "1:30pm",
];
// Prepare array
$periods = [];
// Loop all times in input
foreach($input as $time) {
$hour = substr($time, 0, strpos($time, ':'));
$partOfDay= substr($time, strlen($time) - 2);
$periods[$hour][$partOfDay][] = $time;
}
var_dump($periods);
答案 3 :(得分:0)
这里是功能:
<?php
$input = [
"12:00am",
"12:15am",
"12:30am",
"12:45am",
"1:00am",
"1:15am",
"1:30am",
"1:45am",
"2:00am",
// gap here
"3:15am",
"3:30am",
"3:45am",
"4:00am",
// gap here
"1:00pm",
"1:15pm",
"1:30pm",
];
$period = 15;
$result = split_periods($input, $period);
var_dump($result);
function split_periods($times, $period) {
$standard_diff = $period * 60;
$separated = [];
$last_time = null;
$n = 0;
foreach ($times as $time) {
$current_time = strtotime($time);
if ($last_time) {
$diff = $current_time - $last_time;
if ($diff != $standard_diff) {
++$n;
}
}
$separated[$n][] = $time;
$last_time = $current_time;
}
return $separated;
}
为该函数提供时间数组,以及时间之间的期望间隔(以分钟为单位)。它返回一个数组数组,如下所示。
Array
(
[0] => Array
(
[0] => 12:00am
[1] => 12:15am
[2] => 12:30am
[3] => 12:45am
[4] => 1:00am
[5] => 1:15am
[6] => 1:30am
[7] => 1:45am
[8] => 2:00am
)
[1] => Array
(
[0] => 3:15am
[1] => 3:30am
[2] => 3:45am
[3] => 4:00am
)
[2] => Array
(
[0] => 1:00pm
[1] => 1:15pm
[2] => 1:30pm
)
)
答案 4 :(得分:0)
我使用strtotime()将时间转换为unix时间戳,并计算与下一个元素的差异。
$arr = ['12:00am','12:15am','12:30am','12:45am','1:00am','1:15am','1:30am','1:45am','2:00am','3:15am','3:30am','3:45am','4:00am','1:00pm','1:15pm','1:30pm'];
$new = [];
$temp = [];
$total = count($arr);
for($i=0; $i< $total;$i++){
$k = $i+1;
//900 - it is 15 mins difference
if(isset($arr[$k]) && strtotime($arr[$k]) - strtotime($arr[$i]) == 900){
$temp[] = $arr[$i];
}else{
$temp[] = $arr[$i];
$new[] = $temp;
$temp = [];
}
}
print_r($new);