PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function group_days($block) { $week = array('Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'); $res = []; foreach ($block as $id=>$day) { if ($id == 0) { $period = [$day]; } else { if (array_search($day, $week)-array_search(end($period), $week)==1) { $period[1] = $day; } else { $res[] = $period; $period = [$day]; } } } $res[] = $period; $res = array_map( function($el) {return implode('-', $el);}, $res ); return implode(', ', $res); } $block = array(0 => 'Пн', 1 => 'Ср', 2 => 'Чт', 3 => 'Пт'); print_r(group_days($block)); echo PHP_EOL; $block = array('Пн', 'Вт', 'Ср', 'Пт', 'Сб'); print_r(group_days($block));
Show:  
Copy Clear