How to replace strings in an array in php: You can use the str_replace function to replace, such as [str_replace("red","pink",$arr,$i)]. If the string being searched is an array, then it will find and replace each element in the array.
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
If we need to replace strings in the array, we can use the str_replace() function.
Tip: If the searched string is an array, then it will find and replace each element in the array.
Syntax:
str_replace(find,replace,string,count)
Parameter introduction:
find Required. Specifies the value to look for.
replace Required. Specifies the value to replace the value in find .
#string Required. Specifies the string to be searched for.
#count Optional. A variable counting the number of substitutions.
Code example:
<!DOCTYPE html> <html> <body> <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "<br>" . "Replacements: $i"; ?> <p>In this example, we search an array to find the value "red", and then we replace the value "red" with "pink".</p> </body> </html>
(Learning video sharing: php video tutorial)
Running results:
Related recommendations: php tutorial
The above is the detailed content of How to replace strings in array in php. For more information, please follow other related articles on the PHP Chinese website!