key_exists »
« extract
- PHP-Handbuch
- Funktionsreferenz
- Variablen- und typbezogene Erweiterungen
- Arrays
- Array Funktionen
(PHP 4, PHP 5, PHP 7, PHP 8)
in_array — Prüft, ob ein Wert in einem Array existiert
Beschreibung
in_array(mixed $needle
, array $haystack
, bool $strict
= false
): bool
Diese Funktion sucht in haystack
nach needle
. Wenn strict
gesetzt ist, wird dabei auch der Typ des Elements beachtet.
Parameter-Liste
needle
-
Der gesuchte Wert.
Hinweis:
Ist
needle
ein String, so wird bei der Suche die Groß- und Kleinschreibung beachtet. haystack
-
Das zu durchsuchende Array.
strict
-
Wenn der dritte Parameter auf
true
gesetzt wird, vergleicht in_array() nicht nur den Wert, sondern auch den Typ des gesuchten Wertesneedle
mit den Elementen des Arrays.Hinweis:
Vor PHP 8.0.0 passt ein
needle
-Wert vom Typstring
im nicht-strikten Modus auf einen Array-Wert von0
und umgekehrt. Das kann zu unerwünschten Ergebnissen führen. Ähnliche Grenzfälle gibt es auch für andere Typen. Wenn nicht absolut sicher ist, um welche Art von Werten es sich handelt, sollte immer das Flagstrict
verwendet werden, um ein unerwartetes Verhalten zu vermeiden.
Rückgabewerte
Gibt true
zurück wenn needle
im Array gefunden wird, sonst false
.
Beispiele
Beispiel #1 in_array()-Beispiel
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Irix enthalten";
}
if (in_array("mac", $os)) {
echo "mac enthalten";
}
?>
Der zweite Vergleich schlägt fehl, da in_array() Groß- und Kleinschreibung unterscheidet, die Ausgabe sieht daher so aus:
Irix enthalten
Beispiel #2 in_array()-Beispiel mit 'strict'
if ( if (<?php
$a = array('1.10', 12.4, 1.13);
echo "'12.4' bei strenger Prüfung gefunden\n";
}
echo "1.13 Bei strenger Prüfung gefunden\n";
}
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
1.13 bei strenger Prüfung gefunden
Beispiel #3 in_array() mit Array als Suchwert
if ( if ( if (<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
echo "'ph' gefunden\n";
}
echo "'fi' gefunden\n";
}
echo "'o' gefunden\n";
}
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
'ph' gefunden 'o' gefunden
Siehe auch
- array_search() - Durchsucht ein Array nach einem Wert und liefert bei Erfolg den zugehörigen Schlüssel
- isset() - Prüft, ob eine Variable deklariert ist und sich von null unterscheidet
- array_key_exists() - Prüft, ob ein gegebener Schlüssel in einem Array existiert
Improve This Page
+add a note
User Contributed Notes 8 notes
up
down
409
beingmrkenny at gmail dot com ¶
12 years ago
The solution is to use the strict checking option. <?php// Example array$array = array( // First three make sense, last four do not // Strict checkingLoose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.
'egg' => true,
'cheese' => false,
'hair' => 765,
'goblins' => null,
'ogres' => 'no ogres allowed in this array'
);// Loose checking -- return values are in comments
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false?>
up
down
1
Armands Rieksti ¶
1 year ago
From what I've tested, the function works correctly:I'd like to point out that, if you're using Enum data structures and want to compare whether an array of strings has a certain string Enum in it, you need to cast it to a string.
if the array is filled with strings and you're searching for a string;
if the array is filled with Enums and you're searching for an Enum.
up
down
7
rhill at xenu-directory dot net ¶
15 years ago
<?php $needle echo I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:
'fruit'=>'banana', 'vegetable'=>'carrot'
);$haystack = array(
array('vegetable'=>'carrot', 'fruit'=>'banana'),
array('fruit'=>'apple', 'vegetable'=>'celery')
);
// Output is 'false'echo in_array($needle, $haystack) ? 'true' : 'false';
// Output is 'true'?>
I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.
up
down
1
leonhard dot radonic+phpnet at gmail dot com ¶
1 year ago
<?php // The actual data in my edge-case scenario: It took me quite some time to find out what's going on.I got an unexpected behavior working with in_array. I'm using following code:
// ...
$someId = getSomeId(); // it gets generated/fetched by another service, so I don't know what value it will have. P.S.: it's an integer
// $someId = 0;
// $anyArray = ['dataOne', 'dataTwo'];
// do some work
}
// ...
?>
With PHP7.4, in_array returns boolean true.
With PHP8.1, in_array returns boolean false.
up
down
Julian Sawicki ¶
1 year ago
<?php $myNumbers if ( return Here is a recursive in_array function:
[1,2,3,4,5],
[6,7,8,9,10],
];$array = [
'numbers' => $myNumbers
];// Let's try to find number 7 within $array
$hasNumber = in_array(7, $array, true); // bool(false)
$hasNumber = in_array_recursive(7, $array, true); // bool(true)function in_array_recursive(mixed $needle, array $haystack, bool $strict): bool
{
foreach ($haystack as $element) {
if ($element === $needle) {
return true;
}$isFound = false;
if (is_array($element)) {
$isFound = in_array_recursive($needle, $element, $strict);
}
return true;
}
}
}
up
down
-6
thomas dot sahlin at gmail dot com ¶
14 years ago
<?php $slow if ( if (isset(If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.
print('Found it!');$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange');
print('Found it!');?>
up
down
-5
Anonymous ¶
1 year ago
// Expected: false, got: true // Works fine$a = new StdClass();
$b = new StdClass();
var_dump(in_array($a, [$b]));
// bool(true)
var_dump(in_array($a, [$b], true));
// bool(false)
up
down
-23
Anonymous ¶
1 year ago
// Expected: false, got: true // Works fine$a = new StdClass();
$b = new StdClass();
var_dump(in_array($a, [$b]));
// bool(true)
var_dump(in_array($a, [$b], true));
// bool(false)
+add a note