Page 1 of 1

changing key names

Posted: Tue Jul 27, 2004 9:06 am
by Korvus
hi all!

is there any way of changing an array key name without loosing its internal pointer position?

i've found some functions at php.net but they aren't useful on my case
thx in advance

Posted: Tue Jul 27, 2004 9:26 am
by Weirdan
hm...
something like this perhaps:

Code: Select all

function rename_key($array, $from, $to) {
    $keys = array_keys($array);
    $vals = array_values($array);
    $ret = array();
    for($i = 0, $len = count($keys); $i<$len; $i++) {
        if($keys[$i] == $from) $keys[$i] = $to;
        $ret[ $keys[$i] ] = $vals[$i];
    }
    return $ret;
}