Check if one of multiple values is included - PHP in_array()

How can I check if one or more of the multiple values are included in array() in PHP ?
0
give a positive ratinggive a negative rating
12 Jun 2020 at 03:55 PM
Hi,

To check if at least one of the multiple values is included in PHP array, you can use this solution:

$first_array = array(1,2);
$second_array = array(1,3,5);

if ( array_intersect($first_array, $second_array) ) { echo "There is at least one match."; }
esle { echo "There is no match between arrays."; }

And to check if all of the multiple values are included in PHP array, you can use this solution:

$first_array = array(1,2,3);
$second_array = array(1,3,5,7);

if ( !array_diff($first_array, $second_array) ) { echo "All the values are included."; }
else { "Not all the values are included."; }


0
give a positive ratinggive a negative rating
14 Jun 2020 at 05:23 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
Follow us on Facebook
2022 AnswerTabsTermsContact us