如何编辑数组的键


PHP - How to edit a key of an array?

我有这个数组,我需要编辑数组:

    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];
                    }

知道我的代码中的错误在哪里吗?由于

简单的解决方案是:

$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;
}