Ich versuche, diese Arbeit zu bekommen:PHP Gruppe Array Wochentag/Öffnungszeiten
Ich habe ein Array wie:
//print_r($blocks);
$blocks =
Array (
[0] => Array ([days] => Array ([0] => 1 [1] => 3 [2] => 5) [hours] => Array ([0] => 11:00-20:30))
[1] => Array ([days] => Array ([0] => 2) [hours] => Array ([0] => 11:00-13:00 [1] => 18:00-20:30))
[2] => Array ([days] => Array ([0] => 4 [1] => 6) [hours] => Array ([0] => 10:00-14:30))
)
$ Wochentag => array ('MO', ‚Di "MI", "DO", "FR", "SA", "SO");
MY gewünschten Ausgangs:
return array('Mo, Mi, Fr' => '11:00 - 20:30 Uhr', 'Di' => '11:00 - 13:00 & 18:00 - 20:30 Uhr', 'DO, SA' => '06:00 - 20:30 Uhr');
Funktionen:
private function hours_overview_format_hours(array $ranges)
{
$hoursparts = array();
foreach ($ranges as $range) {
$day = '2016-01-01';
$range = explode('-', $range);
$start = strtotime($day . ' ' . $range[0]);
$end = strtotime($day . ' ' . $range[1]);
$hoursparts[] = date($this->templates['overview_format'], $start)
. $this->templates['overview_separator']
. date($this->templates['overview_format'], $end);
}
return implode($this->templates['overview_join'], $hoursparts);
}
AND:
$ ret = array();
foreach ($blocks as $block){
// Format days
$keyparts = array();
$keys = $block['days'];
$buffer = array();
$lastIndex = null;
$minGroupSize = 3;
foreach ($keys as $index) {
if ($lastIndex !== null && $index - 1 !== $lastIndex){
if (count($buffer) >= $minGroupSize){
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
} else {
foreach ($buffer as $b) {
$keyparts[] = $lookup[$b];
}
}
$buffer = array();
}
$buffer[] = $index;
$lastIndex = $index;
}
if (count($buffer) >= $minGroupSize) {
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
} else {
foreach ($buffer as $b) {
$keyparts[] = $lookup[$b];
}
}
// Combine
$ret[implode(', ', $keyparts)] = $this->hours_overview_format_hours($block['hours']).' Uhr';
}
return $ret;
Aber das funktioniert nicht so wie es sollte. bitte helfen! Danke. Sepp.
Das gibt mir:
Aber sein sollte:
Vielen Dank; Es hat eine Weile gedauert, mit einigen Änderungen in Ihrem Code, ich habe es am Ende funktioniert. Sepp. –