note 72388 added to ref.array

From: Date: Wed, 17 Jan 2007 10:10:45 +0000
Subject: note 72388 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-121827@lists.php.net to get a copy of this message
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 ) ---- Server IP: 69.147.83.197 Probable Submitter: 84.87.192.82 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/72388 Del: integrated -- https://master.php.net/note/delete/72388/integrated Del: useless -- https://master.php.net/note/delete/72388/useless Del: bad code -- https://master.php.net/note/delete/72388/bad+code Del: spam -- https://master.php.net/note/delete/72388/spam Del: non-english -- https://master.php.net/note/delete/72388/non-english Del: in docs -- https://master.php.net/note/delete/72388/in+docs Del: other reasons-- https://master.php.net/note/delete/72388 Reject -- https://master.php.net/note/reject/72388 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121827) next »