示例代碼:
$haystack = array('apple', 'banana', 'orange');
$needle = 'banana';
$key = array_search($needle, $haystack);
if ($key !== false) {
echo "Needle found at index: " . $key;
} else {
echo "Needle not found";
}
示例代碼:
$haystack = array('apple', 'banana', 'orange');
$needle = 'banana';
if (in_array($needle, $haystack)) {
echo "Needle found in the haystack";
} else {
echo "Needle not found";
}
示例代碼:
$haystack = array('apple', 'banana', 'orange', 'banana', 'grape');
$needle = 'banana';
$keys = array_keys($haystack, $needle);
if (!empty($keys)) {
foreach ($keys as $key) {
echo "Needle found at index: " . $key . "\n";
}
} else {
echo "Needle not found";
}