Ask Your Question

How can one use in_array to check for multiple values in PHP?

asked 2022-04-03 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-06-03 21:00:00 +0000

woof gravatar image

To check for multiple values using in_array in PHP, you need to pass an array of values as the needle parameter, and use a loop or array function to check each value. Here is an example:

$values = array('apple', 'banana', 'orange');
$search = array('pear', 'banana');

foreach ($search as $item) {
    if (in_array($item, $values)) {
        echo $item . ' found';
    } else {
        echo $item . ' not found';

In this example, we have an array $values with three fruit names, and another array $search with two fruit names. We loop through the $search array and use in_array to check if each item is found in the $values array. If a value is found, we print a message saying that it was found, otherwise we print a message saying that it was not found.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-04-03 11:00:00 +0000

Seen: 8 times

Last updated: Jun 03 '22