-1

I have an array which consists of 7 days names. This array will be dynamic everytime. So i want to check which day is missing from an array. For ex,

[Monday,Tuesday,Thursday,Friday,Saturday,Sunday]

Here, the wednesday is missing so output should be wednesday

Sometimes there will be more then one day will be missing and sometimes none, so the output should be and array which will contain all missing days.

Kapsonfire
  • 1,013
  • 5
  • 17
NaikNiket
  • 29
  • 1
  • 5

3 Answers3

2

You can use array_diff function to get missing days.

$days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
$inputDays = ['Sunday','Friday'];
$missingDays = array_diff($days,$inputDays);
print_r($missingDays);

Output

Array
(
    [0] => Monday
    [1] => Tuesday
    [2] => Wednesday
    [3] => Thursday
    [5] => Saturday
)

array_diff is case sensitive, you may need to convert string to lower case.

Demo

Shahnawaz Kadari
  • 1,423
  • 1
  • 12
  • 20
2

Since you tagged both and but didn't specify which language you're using for this part of your code, here's a JS solution (which doesn't have a handy array_diff function)

var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var input = ["Sunday","Friday"];
return days.filter(function(day) {return input.indexOf(day) < 0;});
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

you can compare two arrays with array_diff. Example: https://3v4l.org/4g00a

Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58