restaurant_id)->first(); if ($w_form) { $number_slots = $w_form->form_config['number_slots_display']; } else { $number_slots = 4; } } else { $number_slots = $numberOfSlotsToGet; } // Select X slots with the minimum distance $final_slots = []; for ($i = 1; $i <= $number_slots; $i++) { $slot_selected = false; $slot_selected_distance = 100000; // Find the slot with min distance not included in final_slots foreach($arr_slots as $slot) { if (!in_array($slot, $final_slots)) { $slot_distance = abs($slot - $time); if ($slot_distance < $slot_selected_distance) { $slot_selected = $slot; $slot_selected_distance = $slot_distance; } } } if ($slot_selected) { $final_slots[] = $slot_selected; } } asort($final_slots); return $final_slots; } }