我有一个形式为key => value
的关联数组,其中键是数字值,但它不是顺序数字值。实际上,键是一个ID号码,而值是一个计数。这在大多数情况下都很好,但是我想要一个函数,该函数获取数组的可读名称并将其用作键,而不更改值。
我没有看到可以完成此任务的函数,但我假设我需要提供旧键和新键(我都有),并转换数组。有没有一种有效的方法来实现这个?
我有一个形式为key => value
的关联数组,其中键是数字值,但它不是顺序数字值。实际上,键是一个ID号码,而值是一个计数。这在大多数情况下都很好,但是我想要一个函数,该函数获取数组的可读名称并将其用作键,而不更改值。
我没有看到可以完成此任务的函数,但我假设我需要提供旧键和新键(我都有),并转换数组。有没有一种有效的方法来实现这个?
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
如果想保留数组的顺序并进行键名替换,可以将数组的键名放入一个单独的数组中,找到并替换该数组中的键名,然后再将其与值结合起来。
以下是可以实现此操作的函数:
function change_key( $array, $old_key, $new_key ) {
if( ! array_key_exists( $old_key, $array ) )
return $array;
$keys = array_keys( $array );
$keys[ array_search( $old_key, $keys ) ] = $new_key;
return array_combine( $keys, $array );
}
array_splice
:https://dev59.com/b2865IYBdhLWcg3wd-ce - Milos Radojevic不要使用:
"select ´id´ from ´tablename´..."
可以使用类似以下的方式:
"select ´id´ **as NEWNAME** from ´tablename´..."
KernelM的答案很好,但为了避免Greg在评论中提出的问题(冲突键),使用一个新数组会更安全。
$newarr[$newkey] = $oldarr[$oldkey];
$oldarr=$newarr;
unset($newarr);
$array = [
'old1' => 1
'old2' => 2
];
$renameMap = [
'old1' => 'new1',
'old2' => 'new2'
];
$array = array_combine(array_map(function($el) use ($renameMap) {
return $renameMap[$el];
}, array_keys($array)), array_values($array));
/*
$array = [
'new1' => 1
'new2' => 2
];
*/
return isset($renameMap[$el]) ? $renameMap[$el] : $el;
。 - Jerryecho 'Widgets: ' . $data[$humanreadbleMapping['Widgets']];
两种解决方案的简单基准比较。
解决方案1: 复制并删除(顺序丢失,但更快)https://dev59.com/RXVC5IYBdhLWcg3wnCaA#240676
<?php
$array = ['test' => 'value', ['etc...']];
$array['test2'] = $array['test'];
unset($array['test']);
解决方案2:重命名键https://dev59.com/RXVC5IYBdhLWcg3wnCaA#21299719
<?php
$array = ['test' => 'value', ['etc...']];
$keys = array_keys( $array );
$keys[array_search('test', $keys, true)] = 'test2';
array_combine( $keys, $array );
基准测试:
<?php
$array = ['test' => 'value', ['etc...']];
for ($i =0; $i < 100000000; $i++){
// Solution 1
}
for ($i =0; $i < 100000000; $i++){
// Solution 2
}
结果:
php solution1.php 6.33s user 0.02s system 99% cpu 6.356 total
php solution1.php 6.37s user 0.01s system 99% cpu 6.390 total
php solution2.php 12.14s user 0.01s system 99% cpu 12.164 total
php solution2.php 12.57s user 0.03s system 99% cpu 12.612 total
如果你想让新数组键的位置与旧键相同,你可以这样做:
function change_array_key( $array, $old_key, $new_key) {
if(!is_array($array)){ print 'You must enter a array as a haystack!'; exit; }
if(!array_key_exists($old_key, $array)){
return $array;
}
$key_pos = array_search($old_key, array_keys($array));
$arr_before = array_slice($array, 0, $key_pos);
$arr_after = array_slice($array, $key_pos + 1);
$arr_renamed = array($new_key => $array[$old_key]);
return $arr_before + $arr_renamed + $arr_after;
}
$datos = array
(
'0' => array
(
'no' => 1,
'id_maquina' => 1,
'id_transaccion' => 1276316093,
'ultimo_cambio' => 'asdfsaf',
'fecha_ultimo_mantenimiento' => 1275804000,
'mecanico_ultimo_mantenimiento' =>'asdfas',
'fecha_ultima_reparacion' => 1275804000,
'mecanico_ultima_reparacion' => 'sadfasf',
'fecha_siguiente_mantenimiento' => 1275804000,
'fecha_ultima_falla' => 0,
'total_fallas' => 0,
),
'1' => array
(
'no' => 2,
'id_maquina' => 2,
'id_transaccion' => 1276494575,
'ultimo_cambio' => 'xx',
'fecha_ultimo_mantenimiento' => 1275372000,
'mecanico_ultimo_mantenimiento' => 'xx',
'fecha_ultima_reparacion' => 1275458400,
'mecanico_ultima_reparacion' => 'xx',
'fecha_siguiente_mantenimiento' => 1275372000,
'fecha_ultima_falla' => 0,
'total_fallas' => 0,
)
);
这是函数:
function changekeyname($array, $newkey, $oldkey)
{
foreach ($array as $key => $value)
{
if (is_array($value))
$array[$key] = changekeyname($value,$newkey,$oldkey);
else
{
$array[$newkey] = $array[$oldkey];
}
}
unset($array[$oldkey]);
return $array;
}
我喜欢KernelM的解决方案,但我需要处理潜在的键冲突(新键可能与现有键匹配)。这是我想出来的解决方案:
function swapKeys( &$arr, $origKey, $newKey, &$pendingKeys ) {
if( !isset( $arr[$newKey] ) ) {
$arr[$newKey] = $arr[$origKey];
unset( $arr[$origKey] );
if( isset( $pendingKeys[$origKey] ) ) {
// recursion to handle conflicting keys with conflicting keys
swapKeys( $arr, $pendingKeys[$origKey], $origKey, $pendingKeys );
unset( $pendingKeys[$origKey] );
}
} elseif( $newKey != $origKey ) {
$pendingKeys[$newKey] = $origKey;
}
}
$myArray = array( '1970-01-01 00:00:01', '1970-01-01 00:01:00' );
$pendingKeys = array();
foreach( $myArray as $key => $myArrayValue ) {
// NOTE: strtotime( '1970-01-01 00:00:01' ) = 1 (a conflicting key)
$timestamp = strtotime( $myArrayValue );
swapKeys( $myArray, $key, $timestamp, $pendingKeys );
}
// RESULT: $myArray == array( 1=>'1970-01-01 00:00:01', 60=>'1970-01-01 00:01:00' )