我有一个形式key =>值的关联数组,其中key是一个数值,但它不是一个连续的数值。键实际上是一个ID号,值是一个计数。这对于大多数实例来说都很好,但是我想要一个函数获得人类可读的数组名称并将其用作键,而不更改值。
我没有看到这样做的函数,但我假设我需要提供旧键和新键(我都有)并转换数组。有没有一种有效的方法来做到这一点?
我有一个形式key =>值的关联数组,其中key是一个数值,但它不是一个连续的数值。键实际上是一个ID号,值是一个计数。这对于大多数实例来说都很好,但是我想要一个函数获得人类可读的数组名称并将其用作键,而不更改值。
我没有看到这样做的函数,但我假设我需要提供旧键和新键(我都有)并转换数组。有没有一种有效的方法来做到这一点?
当前回答
两种解决方案的简单基准比较。
解决方案1复制和删除(订单丢失,但更快)https://stackoverflow.com/a/240676/1617857
<?php
$array = ['test' => 'value', ['etc...']];
$array['test2'] = $array['test'];
unset($array['test']);
解决方案2密钥重命名为“https://stackoverflow.com/a/21299719/1617857”
<?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
其他回答
这里有一个帮助函数来实现这一点:
/**
* Helper function to rename array keys.
*/
function _rename_arr_key($oldkey, $newkey, array &$arr) {
if (array_key_exists($oldkey, $arr)) {
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
return TRUE;
} else {
return FALSE;
}
}
基于@KernelM的答案。
用法:
_rename_arr_key('oldkey', 'newkey', $my_array);
如果重命名成功,返回true,否则返回false。
这是一个实验(测试)
初始数组(像0、1、2这样的键)
$some_array[] = '6110';//
$some_array[] = '6111';//
$some_array[] = '6210';//
我必须将键名更改为例如human_readable15, human_readable16, human_readable17
类似于已经发布的内容。在每个循环中,我设置必要的键名,并从初始数组中删除相应的键。
例如,我插入mysql $some_array得到lastInsertId,我需要发送键值对返回jquery。
$first_id_of_inserted = 7;//lastInsertId
$last_loop_for_some_array = count($some_array);
for ($current_loop = 0; $current_loop < $last_loop_for_some_array ; $current_loop++) {
$some_array['human_readable'.($first_id_of_inserted + $current_loop)] = $some_array[$current_loop];//add new key for intial array
unset( $some_array[$current_loop] );//remove already renamed key from array
}
这是带有重命名键的新数组
echo '<pre>', print_r($some_array, true), '</pre>$some_array in '. basename(__FILE__, '.php'). '.php <br/>';
如果human_readable15、human_readable16、human_readable17需要其他东西。然后就能创造出这样的东西
$arr_with_key_names[] = 'human_readable';
$arr_with_key_names[] = 'something_another';
$arr_with_key_names[] = 'and_something_else';
for ($current_loop = 0; $current_loop < $last_loop_for_some_array ; $current_loop++) {
$some_array[$arr_with_key_names[$current_loop]] = $some_array[$current_loop];//add new key for intial array
unset( $some_array[$current_loop] );//remove already renamed key from array
}
您可以使用第二个关联数组将人类可读的名称映射到id。这也将提供多对1关系。然后这样做:
echo 'Widgets: '。美元$ data [humanreadbleMapping[“窗口小部件”]];
嗯,我没有测试之前,但我认为这段代码工作
function replace_array_key($data) {
$mapping = [
'old_key_1' => 'new_key_1',
'old_key_2' => 'new_key_2',
];
$data = json_encode($data);
foreach ($mapping as $needed => $replace) {
$data = str_replace('"'.$needed.'":', '"'.$replace.'":', $data);
}
return json_decode($data, true);
}
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);