note 72388 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:08:48 +0000
Subject: note 72388 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199463@lists.php.net to get a copy of this message
Note Submitter: Mark Lindeman ---- Very simple solution for changing the key of an associative array, of course it will fail in a lot of scenarios, but will do the trick in simple arrays: <?php function replace_key(&$input, $from_key, $to_key) { $input = unserialize(str_replace(':"'.$from_key.'";', ':"'.$to_key.'";',serialize($input))); } $array =array("a"=>1, "B"=>2, "c"=>3); replace_key($array, "B", "b"); print_r($array); ?> Output: Array ( [a] => 1 [b] => 2 [c] => 3 )

« previous php.notes (#199463) next »