-4
$array = ['red', 'orange'];

How can I check if this array contains only the values red or orange ?

Thanks.

F__M
  • 1,518
  • 1
  • 19
  • 34
  • [Test if one array is a subset of another](https://stackoverflow.com/q/12276565/2943403) and [how to check multiple $_POST variable for existence using isset()?](https://stackoverflow.com/q/17187066/2943403) and [find all array elements in another array](https://stackoverflow.com/q/6787806/2943403) – mickmackusa Apr 02 '23 at 22:20

1 Answers1

0
$array = ['red', 'orange'];

if (empty(array_diff($array, ['red', 'orange']))) {
  echo "The array contains only red and orange.";
} else {
  echo "The array contains other values besides red and orange.";
}

How about using array_diff() function to compare your array with an array that contains only the values "red" and "orange". If the result is an empty array, it means that your original array only contains those values.

Hello Universe
  • 3,248
  • 7
  • 50
  • 86