Page 1 of 1

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

Posted: Wed Sep 09, 2009 10:06 am
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.

Re: Quick Question About Merging Arrays

Posted: Wed Sep 09, 2009 11:27 am
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.