Home > Mobile >  replace strings in array without loop
replace strings in array without loop

Time:11-25

I have an array like this:

$myArray = array("Value 1", "Value 2");

Now I would like to do a "str_replace", but for an array? How ?

My idea: A foreach loop to get all values, do the str_replace and save the new value to the same array position.

But is there another solution ?

CodePudding user response:

You can supply the array as an argument to str_replace() and it will return an array of the replaced strings:

$newArray = str_replace('Value', 'NewValue', $myArray);
  • Related