在PHP数组中查找字符串的最佳方法? [重复]

This question already has an answer here:

If I have an array that contains some strings like so

$roleNames = ['sales_agent', 'admin'];

And I want to check if that array contains the string 'sales_agent', what would be the best way to go about this with PHP?

Ideally I would like to have the check return a boolean value.

</div>

Easiest Way

$word='admin';
$array = array('sales_agent', 'admin');

if (in_array($word, $array)) {
    echo "admin is in array";
}

You can use PHP's built-in in_array function.

Example:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>