You can "change" the key of an array element simply by setting the new key and removing the old:
您可以通过设置新密钥并删除旧密钥来“更改”数组元素的键:
$array[$newKey] = $array[$oldKey];
unset($array[$oldKey]);
But this will move the key to the end of the array.
但这会将键移动到数组的末尾。
Is there some elegant way to change the key without changing the order?
是否有一些优雅的方式来改变密钥而不改变顺序?
(PS: This question is just out of conceptual interest, not because I need it anywhere.)
(PS:这个问题仅仅是出于概念上的兴趣,不是因为我在任何地方都需要它。)
6 个解决方案
#1
26
Tested and works :)
经过测试和工作:)
$array = array( "a" => "1", "b" => "2", "c" => "3" );
function replace_key($array, $old_key, $new_key) {
$keys = array_keys($array);
if (false === $index = array_search($old_key, $keys)) {
throw new Exception(sprintf('Key "%s" does not exit', $old_key));
}
$keys[$index] = $new_key;
return array_combine($keys, array_values($array));
}
$new_array = replace_key($array, "b", "e");
#2
9
Something like this may also work:
这样的事情也可能有效:
$langs = array("EN" => "English",
"ZH" => "Chinese",
"DA" => "Danish",
"NL" => "Dutch",
"FI" => "Finnish",
"FR" => "French",
"DE" => "German");
$json = str_replace('"EN":', '"en":', json_encode($langs));
print_r(json_decode($json, true));
OUTPUT:
OUTPUT:
Array
(
[en] => English
[ZH] => Chinese
[DA] => Danish
[NL] => Dutch
[FI] => Finnish
[FR] => French
[DE] => German
)
#3
6
One way would be to simply use a foreach iterating over the array and copying it to a new array, changing the key conditionally while iterating, e.g. if $key === 'foo' then dont use foo but bar:
一种方法是简单地使用foreach迭代数组并将其复制到新数组,在迭代时有条件地改变键,例如, if $ key ==='foo'然后不要使用foo但是吧:
function array_key_rename($array, $oldKey, $newKey)
{
$newArray = [];
foreach ($array as $key => $value) {
$newArray[$key === $oldKey ? $newKey : $key] = $value;
}
return $newArray;
}
Another way would be to serialize
the array, str_replace
the serialized key and then unserialize
back into an array again. That isnt particular elegant though and likely error prone, especially when you dont only have scalars or multidimensional arrays.
另一种方法是序列化数组,str_replace序列化的键,然后再次反序列化为一个数组。虽然这并不是特别优雅,但可能容易出错,特别是当你不只有标量或多维数组时。
A third way - my favorite - would be you writing array_key_rename
in C and proposing it for the PHP core ;)
第三种方式 - 我最喜欢的 - 就是你在C中编写array_key_rename并为PHP核心提出它;)
#4
0
We are using this function for changing multiple array keys within an array keeping the order:
我们使用此函数来更改数组中保持顺序的多个数组键:
function replace_keys($array, $keys_map) {
$keys = array_keys($array);
foreach($keys_map as $old_key=>$new_key){
if (false === $index = array_search($old_key, $keys)) {
continue;
}
$keys[$index] = $new_key;
}
return array_combine($keys, array_values($array));
}
You can pass an array as $keys_map, like this:
您可以将数组作为$ keys_map传递,如下所示:
$keys_map=array("old_key_1"=>"new_key_1", "old_key_2"=>"new_key_2",...)
This solution is based on Kristian one.
该解决方案基于Kristian one。
#5
-1
Do a double flip! At least that is all I can think of:
双重翻转!至少这是我能想到的:
$array=("foo"=>"bar","asdf"=>"fdsa");
$array=array_flip($array);
$array["bar"]=>'newkey';
$array=array_flip($array);
#6
-2
You could use array_combine. It merges an array for keys and another for values...
你可以使用array_combine。它合并了一个数组用于键,另一个用于值...
For instance:
例如:
$original_array =('foo'=>'bar','asdf'=>'fdsa');
$new_keys = array('abc', 'def');
$new_array = array_combine($new_keys, $original_array);
#1
26
Tested and works :)
经过测试和工作:)
$array = array( "a" => "1", "b" => "2", "c" => "3" );
function replace_key($array, $old_key, $new_key) {
$keys = array_keys($array);
if (false === $index = array_search($old_key, $keys)) {
throw new Exception(sprintf('Key "%s" does not exit', $old_key));
}
$keys[$index] = $new_key;
return array_combine($keys, array_values($array));
}
$new_array = replace_key($array, "b", "e");
#2
9
Something like this may also work:
这样的事情也可能有效:
$langs = array("EN" => "English",
"ZH" => "Chinese",
"DA" => "Danish",
"NL" => "Dutch",
"FI" => "Finnish",
"FR" => "French",
"DE" => "German");
$json = str_replace('"EN":', '"en":', json_encode($langs));
print_r(json_decode($json, true));
OUTPUT:
OUTPUT:
Array
(
[en] => English
[ZH] => Chinese
[DA] => Danish
[NL] => Dutch
[FI] => Finnish
[FR] => French
[DE] => German
)
#3
6
One way would be to simply use a foreach iterating over the array and copying it to a new array, changing the key conditionally while iterating, e.g. if $key === 'foo' then dont use foo but bar:
一种方法是简单地使用foreach迭代数组并将其复制到新数组,在迭代时有条件地改变键,例如, if $ key ==='foo'然后不要使用foo但是吧:
function array_key_rename($array, $oldKey, $newKey)
{
$newArray = [];
foreach ($array as $key => $value) {
$newArray[$key === $oldKey ? $newKey : $key] = $value;
}
return $newArray;
}
Another way would be to serialize
the array, str_replace
the serialized key and then unserialize
back into an array again. That isnt particular elegant though and likely error prone, especially when you dont only have scalars or multidimensional arrays.
另一种方法是序列化数组,str_replace序列化的键,然后再次反序列化为一个数组。虽然这并不是特别优雅,但可能容易出错,特别是当你不只有标量或多维数组时。
A third way - my favorite - would be you writing array_key_rename
in C and proposing it for the PHP core ;)
第三种方式 - 我最喜欢的 - 就是你在C中编写array_key_rename并为PHP核心提出它;)
#4
0
We are using this function for changing multiple array keys within an array keeping the order:
我们使用此函数来更改数组中保持顺序的多个数组键:
function replace_keys($array, $keys_map) {
$keys = array_keys($array);
foreach($keys_map as $old_key=>$new_key){
if (false === $index = array_search($old_key, $keys)) {
continue;
}
$keys[$index] = $new_key;
}
return array_combine($keys, array_values($array));
}
You can pass an array as $keys_map, like this:
您可以将数组作为$ keys_map传递,如下所示:
$keys_map=array("old_key_1"=>"new_key_1", "old_key_2"=>"new_key_2",...)
This solution is based on Kristian one.
该解决方案基于Kristian one。
#5
-1
Do a double flip! At least that is all I can think of:
双重翻转!至少这是我能想到的:
$array=("foo"=>"bar","asdf"=>"fdsa");
$array=array_flip($array);
$array["bar"]=>'newkey';
$array=array_flip($array);
#6
-2
You could use array_combine. It merges an array for keys and another for values...
你可以使用array_combine。它合并了一个数组用于键,另一个用于值...
For instance:
例如:
$original_array =('foo'=>'bar','asdf'=>'fdsa');
$new_keys = array('abc', 'def');
$new_array = array_combine($new_keys, $original_array);