NYCPHP Meetup

NYPHP.org

[nycphp-talk] Rename an array key?

csnyder chsnyder at gmail.com
Tue Jul 24 12:11:43 EDT 2007


On 7/24/07, David Krings <ramons at gmx.net> wrote:
> csnyder wrote:
> > Anyone know of an easy way to rename an associative array key in place?
> >
> > array( 'vegetable'=>'lettuce', 'fruit'=>'coconut', 'bean'=>'chickpea',
> > 'grain'=>'corn' )
> >
> > I want the third element to be 'legume'=>'chickpea', but it seems to
> > involve rebuilding the array...?
> >
> What you can do is first identify the key/value pair you want to change.
> Then read out the value, then use unset($array['key']), then add the
> saved value to the array with the desired key name.

The trick is that your new key=>value pair will be at the end of the
array, not the third element.

I need to rename a single key "in-place" in a very large array.



More information about the talk mailing list