nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Arrays::renameKey() incorrect replacement for existing new keys. #230

Closed jmwebservices closed 4 years ago

jmwebservices commented 4 years ago

Version: 3.1

Bug Description

When $newKey exsits in $array and is positioned after $oldKey, Arrays::renameKey() preserves the value of $newKey over the value of $oldKey.

Steps To Reproduce

use Nette\Utils\Arrays;

$array  = [ 0 => 0, 1 => 1 ];
$oldKey = 0;
$newKey = 1;
$renamedArray = [ $newKey => $array[ $oldKey ] ];

Arrays::renameKey( $array, $oldKey, $newKey );

var_dump( $array === $renamedArray ); // bool(false)

Expected Behavior

bool(true)

Possible Solution