我需要在关联数组中搜索和替换。

前任:

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

$myarray = array("user1" => "search1", "user2" => "search2", "user3" => "search1" ) ;

我想用 search1 替换 search4 。我怎样才能做到这一点?

更新:我忘了提到该数组有几个 search1 值,我只想更改 key is == $user 所在的值。很抱歉之前没有提到这一点。

最佳答案

$myarray = array("user1" => "search1", "user2" => "search2" );

foreach($myarray as $key => $val)
{
    if ($val == 'search1') $myarray[$key] = 'search4';
}

关于php - 在关联数组内搜索和替换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7142627/

10-11 14:46