in_array

(PHP 4, PHP 5)

in_array -- Controleer of een waarde voorkomt in een array

Beschrijving

bool in_array ( mixed needle, array haystack [, bool strict] )

Doorzoekt haystack voor needle en geeft TRUE terug wanneer deze is gevonden en anders FALSE.

Wanneer de derde strict parameter TRUE is, controleert in_array() ook de types van de needle in de haystack.

Opmerking: Wanneer needle een string is, dan wordt de vergelijking hoofdlettergevoelig uitgevoerd.

Opmerking: In PHP versies lager dan 4.2.0 kon needle geen array zijn.

Voorbeeld 1. in_array() voorbeeld

<?php
$os
= array("Mac", "NT", "Irix", "Linux");
if (
in_array("Irix", $os)) {
    echo
"Got Irix";
}
if (
in_array("mac", $os)) {
    echo
"Got mac";
}
?>

De tweede conditie faalt omdat in_array() hoofdlettergevoelig is, dus het bovenstaande script zal weergeven:

Got Irix

Voorbeeld 2. in_array() met strict voorbeeld

<?php
$a
= array('1.10', 12.4, 1.13);

if (
in_array('12.4', $a, true)) {
    echo
"'12.4' found with strict check\n";
}

if (
in_array(1.13, $a, true)) {
    echo
"1.13 found with strict check\n";
}
?>

Het resultaat van dit script is als volgt:

1.13 found with strict check

Voorbeeld 3. in_array() met een array als needle

<?php
$a
= array(array('p', 'h'), array('p', 'r'), 'o');

if (
in_array(array('p', 'h'), $a)) {
    echo
"'ph' was found\n";
}

if (
in_array(array('f', 'i'), $a)) {
    echo
"'fi' was found\n";
}

if (
in_array('o', $a)) {
    echo
"'o' was found\n";
}
?>

Het resultaat van dit script is als volgt:

'ph' was found
  'o' was found

Zie ook array_search(), array_key_exists() en isset().