- 使用strchr函数查找指定字符在数组中的位置。
示例代码:
$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"; }
- 使用strchr函数判断数组中是否存在指定字符。
示例代码:
$haystack = array('apple', 'banana', 'orange'); $needle = 'banana'; if (in_array($needle, $haystack)) { echo "Needle found in the haystack"; } else { echo "Needle not found"; }
- 使用strchr函数查找指定字符在数组中的所有位置。
示例代码:
$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"; }