php 更新array键值
$arr1 = array("loginname" => "username","psw" => "password");
$arr2 = array("loginname" => "Mike","psw" => 123,"user" => "user123");
//将数组$arr2的‘loginname’‘psw’按照$arr1规定将其替换为新的键名。代码如下:
foreach($arr1 as $key => $value)
{
foreach ($arr2 as $k2=>$v2)
{
if ($key == $k2)
{
unset($arr2[$key]);
$arr2[$value]=$v2;
}
}
}
//打印结果
var_dump($arr2);
var_dump($arr1);
//结果
array (size=3)
‘user‘ => string ‘user123‘ (length=7)
‘username‘ => string ‘Mike‘ (length=4)
‘password‘ => int 123
array (size=2)
‘loginname‘ => string ‘username‘ (length=8)
‘psw‘ => string ‘password‘ (length=8)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。