1

I want to check to see if a voucher is valid for an event. The voucher can be valid on certain days (mo-su) which is stored in an array:

$validOn = [1,3,4]; // Monday, Thursday

An event can be happening on 1 or more days:

$event = [2,6]; // Tuesday, Saturday (unique)

How can I check if the voucher is valid for the given event?

valid([2,6], $validOn); // false
valid([1,6], $validOn); // true
valid([1,4], $validOn); // true
valid([3], $validOn);   // true
etc etc

2 Answers 2

2

You can use array_intersect

function valid($a, $b) {
   return  count(array_intersect($a, $b)) > 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$result = array_intersect($validOn, $event);
if(count($result) > 0) {
  //we have a match
}

See: http://php.net/manual/en/function.array-intersect.php for more information as well.

Comments

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.