What potential pitfalls should be avoided when using array_search in PHP?

One potential pitfall to avoid when using array_search in PHP is that it returns the key of the first occurrence of a value in the array, which can lead to unexpected results if the array contains duplicate values. To ensure you get the correct key for a specific value, you should use strict comparison (===) to compare the search value with the values in the array.

// Correct way to use array_search with strict comparison
$array = [1, 2, 2, 3, 4];
$searchValue = 2;

$key = array_search($searchValue, $array, true); // Use strict comparison

if ($key !== false) {
    echo "Key found: " . $key;
} else {
    echo "Key not found";
}