-2

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.

1
  • 1
    in_array ? if(in_array("sales_agent",$roleNames)) { echo "Hello world!" } Commented Oct 5, 2017 at 14:46

2 Answers 2

0

Easiest Way

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

if (in_array($word, $array)) {
    echo "admin is in array";
}
Sign up to request clarification or add additional context in comments.

Comments

-1

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";
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.