What potential pitfalls should be considered when using array_search() in PHP, especially with regards to keys and positions in the array?

When using array_search() in PHP to find a value in an array, it's important to note that the function returns the key of the first occurrence of the value. If the array contains duplicate values, array_search() may not return the desired key. Additionally, array_search() is case-sensitive, so if you're searching for a string, make sure the case matches. To avoid these issues, consider using array_keys() to get all keys for a given value.

// Using array_keys() to get all keys for a given value
$array = ['apple', 'banana', 'cherry', 'banana'];
$searchValue = 'banana';

$keys = array_keys($array, $searchValue);

foreach ($keys as $key) {
    echo "Key: $key, Value: " . $array[$key] . "\n";
}