0

I have 2 two arrays

$schedule = [
    "Monday" => [0 => "12:00", 1 => "01:20"],
    "Tuesday" => [0 => "04:20",1 => "12:00"],
];

$bookedSlots = [
    ["Monday" => "01:20"],
    ["Tuesday" => "04:20" ] 
];

Now I want the answer or result to return an array of the remaining available slots in which the booked slots should be eliminated from the schedule. like the result given below.

$availableSlots = $schedule - $bookedSlots; // [ "Monday" => [ 0 => "12:00"], "Tuesday" =>[ 0 => "12:00" ];
1
  • Please, share some code that you have already tried. Commented Jun 18, 2020 at 18:16

1 Answer 1

1

Okay. So here I wrote a helper function to generalize the solution. You can use the below-mentioned function.

function find_diff($schedule, $booked_slots)
{
  $diff = [];
  foreach ($schedule as $day =>  $times) {
    $day_wise_slots = isset($booked_slots[$day]) ? $booked_slots[$day] : [];
    if (!is_array($day_wise_slots)) $day_wise_slots = [$day_wise_slots];
    $diff[$day] = array_diff($times, $day_wise_slots);

  }

  return $diff;
}

Functions used: isset and array_diff.

Hope this helps!

Sign up to request clarification or add additional context in comments.

8 Comments

I tried but it is throwing this exception, ErrorException: Array to string conversion at $diff[$day] = array_diff($times, $day_wise_slots);
What are the arguments that you are passing?
I exactly copied your solution. and passed the above arrays mentioned in my question
The array that you passed in the question was in wrong format would throw an error even while passing, I have corrected the question. $schedule = [ "Monday" => [0 => "12:00", 1 => "01:20"], "Tuesday" => [0 => "04:20",1 => "12:00"], ]; $bookedSlots = [ ["Monday" => "01:20"], ["Tuesday" => "04:20" ] ];
I print_r both arrays and here you can see 1drv.ms/u/s!AqcCrPyKckd3tQwJF6uKUd3Ay-hI?e=sDd5gT
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.