in_array() 在陣列中搜尋指定值,找到返回 true,否則 false。第三個參數啟用嚴格模式時,會同時檢查資料型別。
in_array(mixed $needle, array $haystack, bool $strict = false): bool
$needle:要搜尋的值(必需)。$haystack:被搜尋的陣列(必需)。$strict:若為 true,檢查值與型別皆相同;預設 false。字串搜尋時區分大小寫。<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people)) {
echo "Match found";
} else {
echo "Match not found";
}
?>
Match found
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23", $people, TRUE)) {
echo "Match found<br />";
} else {
echo "Match not found<br />";
}
if (in_array("Glenn", $people, TRUE)) {
echo "Match found<br />";
} else {
echo "Match not found<br />";
}
if (in_array(23, $people, TRUE)) {
echo "Match found<br />";
} else {
echo "Match not found<br />";
}
?>
Match not found
Match found
Match found
嚴格模式下 "23" (字串) 不等於 23 (整數),故第一組未匹配。