The in_array Function

The in_array function checks for the presence of a given element in an array.

Syntax

phpin_array(mixed $needle, array $haystack, bool $strict = false): bool

Example

Let's check if the array $arr contains an element with the value 3:

php<?php
  $arr = ['a', 'b', 'c', 'd', 'e'];
  
  $res = in_array('c', $arr);
  var_dump($res);
?>

Code execution result:

phptrue

See Also

  • the array_key_exists function,
    which checks for the presence of a specified key in an array
  • the str_contains function,
    which checks for the occurrence of a character in a string