When I use str_replace with an array as second argument like in the much upvoted and accepted answer to this question, I get an array to string conversion notice. Why?
Example:
$str = 'a b a ba a';
$numerals = range(1, 10);
$str = str_replace('a', $numerals, $str);
gives :
PHP Notice: Array to string conversion in php shell code on line 1
and the following output:
Array b Array bArray Array
instead of
1 b 2 b3 4
You are trying to replace one character (‘a’) with multiple characters (1,2,3,4,5,6,7,8,9,10). PHP can’t figure that out and tries to convert this array to string. When you’re using string as $search parameter you must also use string as $replace parameter. An array as $replace parameter can be used only when $search is also an array. Quoting from documentation:
Here’s the code that will work: