搜索和关联数组内更换 [英] Search and replace inside an associative array
本文介绍了搜索和关联数组内更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要搜索和关联数组内更换。
例如:
$ USER =USER1; //我已经更新了此$ myArray的=阵列(用户1=>中搜索1,用户2=>中搜索2,用户3=>中搜索1);
我要替换搜索1
为 search4
。我怎样才能做到这一点?
更新:我忘了提,数组有几个搜索1
的价值观和我只是想改变的值,其中键== $用户
。对不起,没有提到这点。
解决方案
$ USER =USER1;
$ myArray的=阵列(用户1=>中搜索1,用户2=>中搜索2,用户3=>中搜索1);
的foreach($ myarray中为$关键=> $ VAL)
{
如果($ VAL =='搜索1和$关键== $用户)
{
$ myArray的[$关键] ='search4';
打破;
}
}
的print_r($ myarray的);
打印:
阵列
(
[USER1] => search4
[用户2] =>搜索2
[用户3] =>搜索1
)
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.
解决方案
$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
)
这篇关于搜索和关联数组内更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文