Req #60889 [Wfx]: make array keys changeable
Edit report at https://bugs.php.net/bug.php?id=60889&edit=1
ID: 60889
User updated by: marc-bennewitz at arcor dot de
Reported by: marc-bennewitz at arcor dot de
Summary: make array keys changeable
Status: Wont fix
Type: Feature/Change Request
Package: Arrays related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
You missed out that your simple example puts the changed key at the end of the array. That means if
you need to preserve the order you have to copy the complete array and that's slow on large
arrays.
Previous Comments:
------------------------------------------------------------------------
[2012-01-26 00:25:09] johannes@php.net
Your requested function can be written like this:
function array_change_key(&$arr, $old, $new) {
if (isset($arr[$new]) {
trigger_error(...);
}
$arr[$new] = $arr[$old];
unset($arr[$old]);
}
This creates neither a copy of the array nor of the element ... due to copy on rite. And it's
exactly hat we would do internally.
PHP already has many array funtions and we won't add new ones which can be implemented easily
in userland.
------------------------------------------------------------------------
[2012-01-26 00:06:08] marc-bennewitz at arcor dot de
Description:
------------
On working with maps it's often needed to change the key of an existing element of an array,
but it's currently not possible without a copy of the array or a unset + new element.
Test script:
---------------
// function to change the key
$arr = array('a' => 'a');
array_change_key($arr, 'a', 'b');
var_dump($arr);
echo PHP_EOL . '###########################################' . PHP_EOL;
// function to change the key (warning existing key)
$arr = array('a' => 'a', 'b' => 'b');
array_change_key($arr, 'a', 'b');
var_dump($arr);
echo PHP_EOL . '###########################################' . PHP_EOL;
// function to change the key (overwrite existing element)
$arr = array('a' => 'a', 'b' => 'b');
array_change_key($arr, 'a', 'b', true);
var_dump($arr);
echo PHP_EOL . '###########################################' . PHP_EOL;
// function to change the key (leave existing element)
$arr = array('a' => 'a', 'b' => 'b');
array_change_key($arr, 'a', 'b', false);
var_dump($arr);
echo PHP_EOL . '###########################################' . PHP_EOL;
// foreach key as ref
$arr = array('a' => 'a', 'b' => 'b');
foreach ($arr as &$k => $v) {
$k = 'b';
}
Expected result:
----------------
array(1) {
["b"]=>
string(1) "a"
}
###########################################
Warning: Can't change the array key 'a' to an already existing key 'b'
array(2) {
["a"]=>
string(1) "a",
["b"]=>
string(1) "b"
}
###########################################
array(1) {
["b"]=>
string(1) "a"
}
###########################################
array(1) {
["b"]=>
string(1) "b"
}
###########################################
Warning: Can't change the array key 'a' to an already existing key 'b'
array(2) {
["a"]=>
string(1) "a",
["b"]=>
string(1) "b"
}
Actual result:
--------------
Function related:
There is no funcation to change an array key
foreach related:
PHP Fatal error: Key element cannot be a reference
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60889&edit=1
Thread (3 messages)