我有一个需要编辑数组的数组:
Array
(
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_1\012014.user_1.txt] => TotalVisits 6788
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_1\022014.user_1.txt] => TotalVisits 11141
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_1\032014.user_1.txt] => TotalVisits 6143
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_1\042014.user_1.txt] => TotalVisits 936
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_1\052014.user_1.txt] => TotalVisits 936
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_2\012014.user_2.txt] => TotalVisits 9
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_2\022014.user_2.txt] => TotalVisits 25
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_2\032014.user_2.txt] => TotalVisits 37
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_2\042014.user_2.txt] => TotalVisits 17
[E:\EasyPHP-cmsServer-14.1VC9\data\cms\sites\user_2\052014.user_2.txt] => TotalVisits 16
)我想变成这样:
Array
(
[012014_user_1] => TotalVisits 6788
[022014_user_1] => TotalVisits 11141
[032014_user_1] => TotalVisits 6143
[042014_user_1] => TotalVisits 936
[052014_user_1] => TotalVisits 936
[012014_user_2] => TotalVisits 9
[022014_user_2] => TotalVisits 25
[032014_user_2] => TotalVisits 37
[042014_user_2] => TotalVisits 17
[052014_user_2] => TotalVisits 16
)以下是我尝试过的:
foreach($myarray as $key => $value){
$exp_key = explode('\\', $key);
$exp_key_name = explode('.', $exp_key[6]);
$key = $exp_key_name[0]."_".$exp_key_name[1];
}知道我代码中的错误在哪里吗?谢谢
发布于 2014-05-21 17:07:39
简单的解决办法是:
$array[$newkey] = $array[$oldkey];
unset($array[$oldkey]);在您的例子中,您将遍历:
$newArray = array();
foreach($array as $key => $value) {
$newKey = end(explode("\\", $key)); //need latest php for this otherwise split end and explode
$newArray[$newKey] = $value;
}https://stackoverflow.com/questions/23789620
复制相似问题