Re: changing a key in an array
| From: | Jason k Larson | Date: | Thu, 23 Jan 2003 17:56:13 +0000 |
| Subject: | Re: changing a key in an array | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-132873@lists.php.net to get a copy of this message | ||
If you are looking for something as simple as an index or key rename function it can be done and without making any copies of the array or object etc by:
$array['new_key_name'] =& $array['old_key_name'];
unset($array['old_key_name'];
The first line creates a reference to the array.
The second line removes the first reference without removing the second.
See this for more info:
http://www.php.net/manual/en/language.references.unset.php
Regards,
Jason k Larson
David T-G wrote:
Brent, et al --
...and then Brent Baisley said...
% % I had to do something like this not to long ago. Luckily it hit me that % I shouldn't be changing keys, you don't do it in a database, so you % really shouldn't do it in an array.
Hmmm... Good point.
% I ended up adding a "column" to my array that contained the name. Then I % didn't need to change keys and it worked just as well, and easier.
Can you show me how? Currently I have
Array
(
[a1] => Array
(
[k1] => value1
[k2] => value2
)
[a2] => Array
(
[k1] => value1
[k2] => value2
)
)
and now I'll want to add a 'name' thingie (I daren't call it a column :-)
so that I can change it at will ... but where and how do I add it?
Thanks & TIA & HAND
:-D