From:             
Operating system: 
PHP version:      Irrelevant
Package:          Arrays related
Bug Type:         Feature/Change Request
Bug description:make array keys changeable

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 bug report at https://bugs.php.net/bug.php?id=60889&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=60889&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=60889&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=60889&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=60889&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=60889&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=60889&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=60889&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=60889&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=60889&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=60889&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=60889&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=60889&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=60889&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=60889&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=60889&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60889&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=60889&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=60889&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=60889&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=60889&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=60889&r=mysqlcfg

Reply via email to