views:

9592

answers:

2

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This is fine for most instances, however I want a function that gets the human-readable name of the array and uses that for the key, without changing the value.

I didn't see a function that does this, but I'm assuming I need to provide the old key and new key (both of which I have) and transform the array. Is there an efficient way of doing this?

+3  A: 

You could use a second associative array that maps human readable names to the id's. That would also provide a Many to 1 relationship. Then do something like this:

echo 'Widgets: ' . $data[$humanreadbleMapping['Widgets']];
Tom Ritter
+18  A: 
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
KernelM
Just be careful that 1) No two keys have the same human-readable version 2) No human-readable versions happen to be numbers
Greg