Search and replace inside an associative array

2020-04-11 11:46发布

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.

10条回答
放荡不羁爱自由
2楼-- · 2020-04-11 12:07

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.

查看更多
We Are One
3楼-- · 2020-04-11 12:14

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;
}
查看更多
我命由我不由天
4楼-- · 2020-04-11 12:15
$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
)
查看更多
Ridiculous、
5楼-- · 2020-04-11 12:18

Why not just do

if (isset($myarray[$user])) $myarray[$user] = 'search4';
查看更多
我想做一个坏孩纸
6楼-- · 2020-04-11 12:18

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';
}
查看更多
7楼-- · 2020-04-11 12:19
$myarray = array("user1" => "search1", "user2" => "search2" );

foreach($myarray as $key => $val)
{
    if ($val == 'search1') $myarray[$key] = 'search4';
}
查看更多
登录 后发表回答