[DW - solved!!]Quick Question About Merging Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

[DW - solved!!]Quick Question About Merging Arrays

Post by jackpf »

Ok,
So basically, for my template class I can assign variables right? yeah. These variables are held in the private property $vars. Now, the assign_vars() function basically consists of this:

Code: Select all

$this->vars += $vars; //$vars being the argument passed. it's basically the same as $this->vars = array_merge($this->vars, $vars);
But, it appears that old values are not overwritten with new ones when assigning variables that have already been assigned.

I've had a poke in the manual, and found that array_merge() (or += for arrays) does not overwrite pre-existing array keys with new values.

So, short of putting the whole thing into a foreach() loop and assigning the variables one by one, is there any way to force array_merge() (or +=) to overwrite existing array keys with new values??

Any help appreciated :)

Thanks,
Jack.
Last edited by jackpf on Wed Sep 09, 2009 11:28 am, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Quick Question About Merging Arrays

Post by jackpf »

Oh my god...I figured it out.

Basically, I was assigning these variables to the template in a loop...so the template obviously only gets the last variable set...which is in fact the same as the original one. But it gets changed somewhere around the middle...and I was wondering why it wasn't changing.

But yeah, thanks for the info!! I did originally think that the array on the right took precedence...but when the variables were supposedly "not being set" I thought that maybe += would work...

But yeah, thanks for the information :)

Cheers,
Jack.
Post Reply