NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - array_search

Trieda

Koreň \ Bez triedy

Metóda - array_search

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

Searches the array for a given element. We talk about the problem as about searching for a needle in a haystack.

Procedurálne

  • function array_search (mixed $needle, array $haystack, bool $strict = false) : mixed

Parametre

NázovDátový typPredvolená hodnotaPopis
$needlemixed

A needle (a string to find).

$haystackarray

A haystack (where to search).

$strictbool false

The third optional parameter is a type of boolean and it specifies if we want to strictly compare elements using ===.

Mávratovej hodnoty

Vracia: mixed

A key under which a needle is found. It returns FALSE in the case of failure. As the needle can be found on the position 0, the result must be compared using ===. If invalid parameters are passed, it returns NULL.

Príklady

<?php
$array = ['Homer', 'Marge', 'Bart', 'Lisa', 'Meggie'];
$key = array_search('Bart', $array);

echo $key;

Súvisiace manuály

      • function array_keys (array $array, mixed $search_value = null, bool $strict = false) : array
      • function array_key_exists (mixed $key, array $array) : bool
      • function array_values (array $array) : array
      • function in_array (mixed $needle, array $haystack, bool $strict = false) : bool
      Aktivity