Search and replace inside an associative array

后端 未结 10 1856
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 22:14

I need to search and replace inside an associative array.

ex:

$user = \"user1\"; // I\'ve updated this

$myarray = array(\"user1\" => \"search1\",         


        
10条回答
  •  悲哀的现实
    2021-01-20 22:47

    Following on from Joseph's answer, using preg_replace may enable you to use the code in other situations:

    function pregReplaceInArray($pattern,$replacement,$array) {
        foreach ($array as $key => $value) {
            $array[$key] = preg_replace($pattern,$replacement,$value);
        }   
        return $array;
    }
    

提交回复
热议问题