Replace string in an array with PHP

后端 未结 6 616
傲寒
傲寒 2020-12-09 17:57

How can I replace a sub string with some other string for all items of an array in PHP?

I don\'t want to use a loop to do it. Is there a predefined function in PHP t

6条回答
  •  情深已故
    2020-12-09 18:35

    I am not sure how efficient this is, but I wanted to replace strings in a big multidimensional array and did not want to loop through all items as the array structure is pretty dynamic.

    I first json_encode the array into a string.

    Replace all the strings I want (need to use preg_replace if there are non-English characters that get encoded by json_encode).

    json_decode to get the array back.

提交回复
热议问题