Is there a code that looks for values ​​in one array and changes the values ​​of a string by taking values ​​from another array? Is it possible to integrate the search data and the replacement data into one asoc? an array?

Here is the code:

$a = ['plus', 'minus']; $b = ['+', '-']; echo str_replace($a, $b, 'plus minus plus'); //Можно ли это как то переписать под асоц. массив? //$a = ['plus'=>'+', 'minus'=>'-']; //echo magic($a, 'plus minus plus'); 

    1 answer 1

    For example using strtr :

     $a = ['plus'=>'+', 'minus'=>'-']; echo strtr('plus minus plus', $a); 

    But you have to be careful with him sometimes. See examples, see what the trick can sometimes be: http://php.net/manual/ru/function.strtr.php#refsect1-function.strtr-examples

    Slightly more complex solution, but there is a place to be:

     $a = ['plus'=>'+', 'minus'=>'-']; $result = str_replace( array_keys($a), array_values($a), 'plus minus plus' ); echo $result;