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 ?
12 Jun 2020 at 03:55 PM
0give a positive ratinggive a negative ratingshow more
onefiveonefive
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."; }


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