How does the in_array function in PHP treat special characters like line breaks?

Special characters like line breaks may cause issues when using the in_array function in PHP because it compares values strictly, including special characters. To solve this issue, you can use the array_map function with the trim function to remove any leading or trailing whitespace and special characters from the array values before performing the in_array check.

$array = ["apple\n", "banana", "cherry"];
$search = "apple\n";

$cleanedArray = array_map('trim', $array);

if (in_array(trim($search), $cleanedArray)) {
    echo "Value found in array!";
} else {
    echo "Value not found in array.";
}