2017-02-05 2 views
0

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:

enter image description here

Aber sein sollte:

enter image description here

Antwort

0

OK, ich glaube, ich eine Funktion haben, die das Array in das gewünschte Format zurückgibt.

dieser Satz von Eingangsblöcken Given (von Ihrem Beispiel):

$blocks = [ 
    0 => [ 
    'days' => [ 
     1, 3, 5 
    ], 
    'hours' => [ 
     '11:00-20:30' 
    ] 
    ], 
    1 => [ 
    'days' => [ 
     2 
    ], 
    'hours' => [ 
     '11:00-13:00', 
     '18:00-20:30' 
    ] 
    ], 
    2 => [ 
    'days' => [ 
     4, 6 
    ], 
    'hours' => [ 
     '10:00-14:30' 
    ] 
    ], 
]; 

Und diese Reihe von Tagen (wieder von Ihrem Beispiel):

$days = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']; 

Mit dieser Funktion:

function formatBlocks($blocks, $days) { 

    $dayArray = []; 
    $hourArray = []; 

    foreach ($blocks as $block) { 

    foreach ($block['days'] as $day) { 
     $dayArray[] = $days[$day - 1]; 
    } 

    $dayString = implode(', ', $dayArray); 

    foreach ($block['hours'] as $hour) { 
     $hourArray[] = $hour; 
    } 

    $hourString = implode(' & ', $hourArray); 
    $hourString .= ' Uhr'; 

    $finalArray[$dayString] = $hourString; 
    $dayArray = []; 
    $hourArray = []; 
    } 

    return $finalArray; 
} 

Und wenn so genannt:

$result = formatBlocks($blocks, $days); 

var_dump($result); 

Das Ergebnis wird sein:

array (size=3) 
    'Mo, Mi, Fr' => string '11:00-20:30 Uhr' (length=15) 
    'Di' => string '11:00-13:00 & 18:00-20:30 Uhr' (length=29) 
    'Do, Sa' => string '10:00-14:30 Uhr' (length=15) 

Mit dem Array in diesem Format, sollten Sie in der Lage sein, es in stecken Sie den Ausgang, den Sie wollen zu bekommen. Wenn nicht, lassen Sie es mich wissen und ich werde an etwas arbeiten, um die gewünschte Ausgabe zu erhalten.

+0

Vielen Dank; Es hat eine Weile gedauert, mit einigen Änderungen in Ihrem Code, ich habe es am Ende funktioniert. Sepp. –