PrimitiveType

PHP: check array contains a value

Use PHP's in_array() function to check an array contains a value:

<?php $myArray = array('guitar', 'piano', 'saxophone', 'trumpet', 'violin'); if (in_array('trumpet', $myArray)) { echo "Array contains 'trumpet'" . PHP_EOL; // will be printed } if (in_array('banjo', $myArray)) { echo "Array contains 'banjo'" . PHP_EOL; // will not be printed }