我正在从我的数据库中循环一些开放时间,并且我已经能够将时间戳合并为基于一周中7天的数组。例如
array(7) {
[1]=>
string(28) "12:00 - 14:00, 16:00 - 23:30"
[2]=>
string(13) "12:00 - 23:30"
[3]=>
string(13) "12:00 - 23:30"
[4]=>
string(13) "12:00 - 23:30"
[5]=>
string(13) "12:00 - 23:30"
[6]=>
string(13) "12:00 - 23:30"
[7]=>
string(13) "12:00 - 23:30"
}
我想知道如何将上面的数组合并到这样的数组中:
array(2) {
['Mon']=>
string(28) "12:00 - 14:00, 16:00 - 23:30"
['Tue - Sun']=>
string(13) "12:00 - 23:30"
}
我的冲突是我有时会有这样的开放时间:
array(7) {
[1]=>
string(13) "12:00 - 23:30"
[2]=>
string(28) "12:00 - 14:00, 16:00 - 20:00"
[3]=>
string(28) "12:00 - 14:00, 16:00 - 20:00"
[4]=>
string(13) "12:00 - 23:30"
[5]=>
string(13) "12:00 - 23:30"
[6]=>
string(13) "12:00 - 23:30"
[7]=>
string(1) "-" // Means closed
}
结果应该是这样的:
array(2) {
['Mon']=>
string(13) "12:00 - 23:30"
['Tue & Wed']=>
string(28) "12:00 - 14:00, 16:00 - 20:00"
['Thu - Sat']=>
string(13) "12:00 - 23:30"
['Sun']=>
string(6) "Closed"
}
到目前为止,这是我的功能:
function mergeOpeningHours($arr) {
$days = array();
$day_name = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
// Firstly we merge the days timestamps
foreach ($arr as $day => $data) {
if (isset($days[$data['day']])) {
// then the day has already been defined and we procedd by applying the days following timestamps
$days[$data['day']] .= ', ' . $data['open'] . ' - ' . $data['close'];
} else {
$days[$data['day']] = $data['open'] . ' - ' . $data['close'];
}
}
$final_days = array();
$d = 1;
$dd = $days[$d];
for ($i=1; $i < 7; $i++) {
if ($days[$d+1] == $dd) {
$final_days[$day_name[$d] . ' - ' . $day_name[$d + 1]] = $dd;
} else {
$dd = $days[$i+1];
}
$d++;
}
}
出现了这样的事情:
array(5) {
["Wed - Thu"]=>
string(13) "12:00 - 23:30"
["Thu - Fri"]=>
string(13) "12:00 - 23:30"
["Fri - Sat"]=>
string(13) "12:00 - 23:30"
["Sat - Sun"]=>
string(13) "12:00 - 23:30"
["Sun - "]=>
string(13) "12:00 - 23:30"
}
非常感谢任何建议 - 谢谢!
**解决方案**
感谢Stackoverflow社区的大量帮助,我设法将所需的功能集合在一起。
function mergeOpeningHours($arr) {
$days = array();
// Firstly we merge the days timestamps
foreach ($arr as $day => $data) {
if (isset($days[$data['day']])) {
// then the day has already been defined and we procedd by applying the days following timestamps
$days[$data['day']] .= ', ' . $data['open'] . ' - ' . $data['close'];
} else if (empty($data['open']) || empty($data['close']) || $data['open'] == NULL || $data['close'] == NULL) {
$days[$data['day']] = '-';
} else {
$days[$data['day']] = $data['open'] . ' - ' . $data['close'];
}
}
$week = array (1 => 'Mon', 2 => 'Tue', 3 => 'Wed', 4 => 'Thu', 5 => 'Fri', 6 => 'Sat', 7 => 'Sun');
$newarray = $finalarray = $valArray = array();
$i = $e = 0;
foreach ($days as $key => $val) {
if (isset($newarray[$e][$val]) && count($newarray[$e][$val]) > 0 && ((int)$key - 1 == end($newarray[$e][$val]))) {
$newarray[$e][$val][$i] = $key;
$i++;
} else {
$e++;
$i = 0;
$newarray[$e][$val][$i] = $key;
$i++;
}
}
foreach ($newarray as $k => $v) {
foreach ($v as $nk => $nv) {
$cnt = count($nv);
if ($cnt > 2) {
$finalarray[$week[array_shift($nv)].' - '.$week[array_pop($nv)]] = $nk;
} elseif ($cnt == 2) {
$finalarray[$week[array_shift($nv)].' & '.$week[array_pop($nv)]] = $nk;
} else {
$finalarray[$week[$nv[0]]] = $nk;
}
}
}
return $finalarray;
}
它可以采用这样的数组:
array(9) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["day"]=>
string(1) "1"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "23:30"
}
[1]=>
array(4) {
["id"]=>
string(1) "2"
["day"]=>
string(1) "2"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "14:00"
}
[2]=>
array(4) {
["id"]=>
string(1) "3"
["day"]=>
string(1) "3"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "14:00"
}
[3]=>
array(4) {
["id"]=>
string(1) "4"
["day"]=>
string(1) "4"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "23:30"
}
[4]=>
array(4) {
["id"]=>
string(1) "5"
["day"]=>
string(1) "5"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "23:30"
}
[5]=>
array(4) {
["id"]=>
string(1) "6"
["day"]=>
string(1) "6"
["open"]=>
string(5) "12:00"
["close"]=>
string(5) "23:30"
}
[6]=>
array(4) {
["id"]=>
string(1) "7"
["day"]=>
string(1) "7"
["open"]=>
string(0) ""
["close"]=>
string(0) ""
}
[7]=>
array(4) {
["id"]=>
string(1) "8"
["day"]=>
string(1) "2"
["open"]=>
string(5) "16:00"
["close"]=>
string(5) "20:00"
}
[8]=>
array(4) {
["id"]=>
string(1) "9"
["day"]=>
string(1) "3"
["open"]=>
string(5) "16:00"
["close"]=>
string(5) "20:00"
}
}
并将其转换为:
array(4) {
["Mon"]=>
string(13) "12:00 - 23:30"
["Tue & Wed"]=>
string(28) "12:00 - 14:00, 16:00 - 20:00"
["Thu - Sat"]=>
string(13) "12:00 - 23:30"
["Sun"]=>
string(1) "-"
}
感谢所有的帮助和支持..
答案 0 :(得分:1)
尝试这样的事情:
$week = array (1 => 'Mon', 2=> 'Tue', ....);
$newarray = $finalarray = $valArray = array();
$i = 0;
foreach ($array as $key => $val) {
if (count($newarray[$val]) > 0 && ((int)$key - 1 == end($newarray[$val]))) {
$newarray[$i][] = $key;
$valArray[$i] = $val;
$i = $i +1;
}
}
foreach ($newarray as $k => $v) {
$cnt = count($v);
if ($cnt > 2) {
$finalarray[$week[array_shift($v)].' - '.$week[array_pop($v)]] = $valArray[$k];
} elseif ($cnt == 2) {
$finalarray[$week[array_shift($v)].' & '.$week[array_pop($v)]] = $valArray[$k];
} else {
$finalarray[$week[$v[0]]] = $valArray[$k];
}
}
答案 1 :(得分:0)
我得到了这个:
$array=array("
12:00 - 14:00, 16:00 - 23:30",
"12:00 - 23:30",
"12:00 - 23:30",
"12:00 - 23:30"
,"12:00 - 23:30"
,"12:00 - 23:30"
,"12:00 - 23:30"
,"12:00 - 14:00"
);
$days=array(0=>'Mon',1=>'Tuesday',2=>'Wed',3=>'Thu',4=>'Fri',5=>'Sat',6=>'Sun');
//var_dump($array);
//var_dump($days);
$previousvalue=0;
for($i=0;$i<7;$i++){
if($i!=0){
if($array[$i]==$previousvalue){
echo $days[$i].'=>'.$array[$i].'<br>';
}else{
}
}
$previousvalue=$array[$i];
}
此代码仅回显与之前代码相同的日期。因此,我基本上将其他日子分开,与相互联系的日子是连续的并且有相同的时间。这可能会有所帮助。现在我需要的是将具有相同值的日期合并为:连续的第一天 - 连续的最后一天。我会在找到的东西后立即编辑。
注意:强>
我的代码从索引0开始,而你的代码从1开始