arrays - Sorting opening hours in PHP -
i have array opening hours structured this:
array:7 [ "mon" => array [ "open" => null "close" => null ] "tue" => array [ "open" => null "close" => null ] "wed" => array [ "open" => "09:00" "close" => "20:00" ] "thu" => array [ "open" => null "close" => null ] "fri" => array [ "open" => "14:00" "close" => "17:00" ] "sat" => array [ "open" => "12:00" "close" => "15:00" ] "sun" => array [ "open" => "12:00" "close" => "15:00" ] ]
i create structure this:
array [ array [ "mon" => array [ "open" => null "close" => null ] "tue" => array [ "open" => null "close" => null ] ] array [ "wed" => array [ "open" => "09:00" "close" => "20:00" ] ] array [ "thu" => array [ "open" => null "close" => null ] ] array [ "fri" => array [ "open" => "14:00" "close" => "17:00" ] ] array [ "sat" => array [ "open" => "12:00" "close" => "15:00" ] "sun" => array [ "open" => "12:00" "close" => "15:00" ] ] ]
where days same opening hours grouped long days in row. otherwise day should end itself.
i've been working on achieving in php , i've got far.
$hours = []; $previousday = null; foreach ($days $key => $day) { if ($day === $previousday) { $hours[] = array($key => $day); } else { $hours[] = array($key => $day); } $previousday = $day; }
i'm stuck on how group days same opening hours in 1 array.
how below?
$result = array(); $previous = null; $idx = -1; foreach ($days $key => $day){ if($day !== $previous){ $idx++; } $result[$idx][$key] = $day; $previous = $day; }
Comments
Post a Comment