How can the use of str_replace() in PHP be optimized to avoid unnecessary code?

When using str_replace() in PHP, unnecessary code can be avoided by optimizing the way replacements are made. One way to do this is by passing arrays of search and replace strings to str_replace() instead of making multiple individual calls to the function. This can reduce the amount of code written and improve performance.

// Example of optimizing str_replace() by passing arrays of search and replace strings
$search = array('red', 'green', 'blue');
$replace = array('pink', 'lime', 'navy');
$text = 'The sky is blue, the grass is green, and the apple is red.';
$new_text = str_replace($search, $replace, $text);
echo $new_text;