码迷,mamicode.com
首页 > Web开发 > 详细

php 更新array键值

时间:2015-02-23 20:07:29      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:

$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)

php 更新array键值

标签:

原文地址:http://www.cnblogs.com/dlbird/p/4298224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!