Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

array_search()

Devolve a chave do primeiro valor correspondente ou false se não existir.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

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

Devolve a chave do primeiro valor correspondente ou false se não existir. O terceiro argumento true exige igualdade de tipo e valor. A chave 0 é um resultado válido, independentemente da posição da entrada. Verifique a falha com === false.

Assinatura

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

Saída do exemplo

[0,1,false]

Exemplos

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

Resultado

[0,1,false]

Fontes