How can in_array() be combined with str_replace() to achieve specific replacements in PHP arrays?

To achieve specific replacements in PHP arrays using in_array() and str_replace(), you can iterate through the array using a loop, check if each element exists in the array using in_array(), and then use str_replace() to perform the replacement if the element is found. This allows you to target specific values in the array for replacement while maintaining the original array structure.

$array = ["apple", "banana", "cherry", "date"];
$search = "banana";
$replace = "orange";

foreach($array as $key => $value){
    if(in_array($search, $array)){
        $array[$key] = str_replace($search, $replace, $value);
    }
}

print_r($array);