在关联数组内搜索和替换

发布于 2024-11-30 11:43:13 字数 378 浏览 1 评论 0原文

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

例如:

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

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

我想将 search1 替换为 search4。我怎样才能实现这个目标?

更新:我忘记提及该数组有多个 search1 值,我只想更改 key == $user 处的值。抱歉没有早点提到这一点。

I need to search and replace inside an associative array.

ex:

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

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

I want to replace search1 for search4. How can I achieve this?

UPDATE: I forgot to mention that the array has several search1 values and I just want to change the value where the key is == $user. Sorry for not mention this earlier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

长安忆 2024-12-07 11:43:14

更新

自从帖子更新后,我有机会睡一觉,我意识到我的回答很愚蠢。如果您有给定的键并且需要更改它的值,为什么要迭代整个数组呢?

$user    = 'user1';
$search  = 'search1';
$replace = 'search4';
$array   = array('user1' => 'search1', 'user2' => 'search2');

if (isset($array[$user]) && $search === $array[$user]) $array[$user] = $replace;

与 @Joseph 的方法类似(几乎相同),但有一些调整:

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

foreach($array as $key => &$value) {
    if ($key === $user) {
        $value = 'search4';
        break; // Stop iterating after key has been found
    }
}

通过引用传递是在 foreach 内编辑的更好方法,并且可以说更快。

Updated

Since the post was updated, and I have had chance to get some sleep, I realized my answer was stupid. If you have a given key and you need to change it's value, why iterate over the whole array?

$user    = 'user1';
$search  = 'search1';
$replace = 'search4';
$array   = array('user1' => 'search1', 'user2' => 'search2');

if (isset($array[$user]) && $search === $array[$user]) $array[$user] = $replace;

Similar to @Joseph's method (pretty much the same), but with a few tweaks:

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

foreach($array as $key => &$value) {
    if ($key === $user) {
        $value = 'search4';
        break; // Stop iterating after key has been found
    }
}

Passing by reference is a nicer way to edit inside foreach, and is arguably quicker.

天生の放荡 2024-12-07 11:43:14

在关联数组或数字中搜索和替换
替换任何关联数组中的值,并且数组可以是任何深度

function array_value_replace($maybe_array, $replace_from, $replace_to) {

    if (!empty($maybe_array)) {
        if (is_array($maybe_array)) {
            foreach ($maybe_array as $key => $value) {
                $maybe_array[$key] = array_value_replace($value, $replace_from, $replace_to);
            }
        } else {
            if(is_string($maybe_array)){
                $maybe_array = str_replace($replace_from, $replace_to, $maybe_array);
            }               
        }
    }

    return $maybe_array;
}

Search and replace inside an associative array or numeric
Replace value in any associative array and array can be any deep

function array_value_replace($maybe_array, $replace_from, $replace_to) {

    if (!empty($maybe_array)) {
        if (is_array($maybe_array)) {
            foreach ($maybe_array as $key => $value) {
                $maybe_array[$key] = array_value_replace($value, $replace_from, $replace_to);
            }
        } else {
            if(is_string($maybe_array)){
                $maybe_array = str_replace($replace_from, $replace_to, $maybe_array);
            }               
        }
    }

    return $maybe_array;
}
一曲琵琶半遮面シ 2024-12-07 11:43:14

如果您想要特定的密钥,那么您只需在前面的示例中添加密钥条件即可。

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

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

if you want for particular key then you just add condition for key in previous ans like.

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

foreach($myarray as $key => $val)
{
    if ($val == 'search1' && $key == $user) $myarray[$key] = 'search4';
}
丘比特射中我 2024-12-07 11:43:14

使用 str_replace 应该可以:

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

$newArray = str_replace('search1', 'search4', $myarray);

Using str_replace should work:

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

$newArray = str_replace('search1', 'search4', $myarray);
甜柠檬 2024-12-07 11:43:14

根据约瑟夫的回答,使用 preg_replace 可能会启用您可以在其他情况下使用该代码:

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

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;
}
携君以终年 2024-12-07 11:43:13
$myarray = array("user1" => "search1", "user2" => "search2" );

foreach($myarray as $key => $val)
{
    if ($val == 'search1') $myarray[$key] = 'search4';
}
$myarray = array("user1" => "search1", "user2" => "search2" );

foreach($myarray as $key => $val)
{
    if ($val == 'search1') $myarray[$key] = 'search4';
}
情场扛把子 2024-12-07 11:43:13

有一个函数可以实现此目的:array_map()

// Using a lamba function, PHP 5.3 required
$newarray = array_map(
    function($v) { if ($v == 'search1') $v = 'search4'; return $v; },
    $myarray
);

如果您不想使用 lambda 函数,请定义一个普通函数或方法并对其进行回调。

There's a function for this : array_map().

// Using a lamba function, PHP 5.3 required
$newarray = array_map(
    function($v) { if ($v == 'search1') $v = 'search4'; return $v; },
    $myarray
);

If you don't want to use a lambda function, define a normal function or method and callback to it.

傲影 2024-12-07 11:43:13

为什么不直接做

if (isset($myarray[$user])) $myarray[$user] = 'search4';

Why not just do

if (isset($myarray[$user])) $myarray[$user] = 'search4';
烟火散人牵绊 2024-12-07 11:43:13
$user = "user1";
$myarray = array("user1" => "search1", "user2" => "search2", "user3" => "search1" );
foreach($myarray as $key => $val)
{
    if ($val == 'search1' and $key == $user )
    {
       $myarray[$key] = 'search4';
       break;
    }
}
print_r($myarray);

印刷:

Array
(
    [user1] => search4
    [user2] => search2
    [user3] => search1
)
$user = "user1";
$myarray = array("user1" => "search1", "user2" => "search2", "user3" => "search1" );
foreach($myarray as $key => $val)
{
    if ($val == 'search1' and $key == $user )
    {
       $myarray[$key] = 'search4';
       break;
    }
}
print_r($myarray);

Prints:

Array
(
    [user1] => search4
    [user2] => search2
    [user3] => search1
)
不气馁 2024-12-07 11:43:13
$originalArray = array( "user1" => "search1" , "user2" => "search2" );
$pattern = 'search1';
$replace = 'search4';

$replacedArray = preg_replace( '/'.$pattern.'/' , $replace , $originalArray );

修复了响应此解决方案

$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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文