What are potential pitfalls to avoid when using arrays in conjunction with preg_replace in PHP?

One potential pitfall to avoid when using arrays in conjunction with preg_replace in PHP is not properly handling the replacement values. If the replacement values are not correctly matched with the corresponding pattern matches, it can result in unexpected output or errors. To avoid this issue, make sure that the replacement values in the array are in the correct order to match the patterns in the regular expression.

// Example of properly handling replacement values in preg_replace with arrays
$patterns = array('/apple/', '/orange/');
$replacements = array('banana', 'grape');
$string = 'I like apple and orange.';
$new_string = preg_replace($patterns, $replacements, $string);
echo $new_string;