Search and replace inside an associative array

后端 未结 10 1861
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  猫巷女王i
    2021-01-20 22:53

    $originalArray = array( "user1" => "search1" , "user2" => "search2" );
    $pattern = 'search1';
    $replace = 'search4';
    
    $replacedArray = preg_replace( '/'.$pattern.'/' , $replace , $originalArray );
    

    Fixes the risk mentioned in comment in response to this solution

提交回复
热议问题