Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_search()

Return the key of the first matching value or false if no match exists.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Return the key of the first matching value or false if no match exists. The optional strict mode also checks the type. Key zero is valid at any position, so compare the result strictly with false to detect a failed search.

Signature

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

Example output

[0,1,false]

Examples

<?php
$items = ['7', 7, 8];
echo json_encode([array_search(7, $items), array_search(7, $items, true), array_search(9, $items, true)]);

Result

[0,1,false]

Sources