in_array()函数
$array = ['apple', 'banana', 'orange']; $character = 'banana'; if (in_array($character, $array)) { echo '包含该字符'; } else { echo '不包含该字符'; }
array_search()函数
$array = ['apple', 'banana', 'orange']; $character = 'banana'; if (array_search($character, $array) !== false) { echo '包含该字符'; } else { echo '不包含该字符'; }
foreach循环遍历数组
$array = ['apple', 'banana', 'orange']; $character = 'banana'; $contains = false; foreach ($array as $item) { if ($item == $character) { $contains = true; break; } } if ($contains) { echo '包含该字符'; } else { echo '不包含该字符'; }
array_intersect()函数
$array = ['apple', 'banana', 'orange']; $character = 'banana'; $intersect = array_intersect([$character], $array); if (!empty($intersect)) { echo '包含该字符'; } else { echo '不包含该字符'; }