148 of 410 menu

The array_search Function

The array_search function performs a search for a value in an array and returns the key of the first found element. If such an element is not found - it returns false. The third parameter sets strict type comparison (like ===). If set to true - it will compare strictly, and if false (by default) - then not.

Syntax

array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false

Example

Let's find an element with the value 'c' in the array - as a result, we will get its key (it is equal to 2):

<?php $arr = ['a', 'b', 'c', 'd', 'e']; echo array_search('c', $arr); ?>

Code execution result:

2

See Also

  • the strpos function,
    which returns the position of the first occurrence of a substring
  • the str_contains function,
    which checks for the occurrence of a character in a string
  • the str_starts_with function,
    which checks the beginning of a string
  • the str_ends_with function,
    which checks the end of a string
byenru