Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Assigning return values in an array

What am I missing here?

12:27 pm on Jul 17, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 13, 2003
posts: 151
votes: 0

I'm getting weird behaviour when iterating over an array and creating a new array. On each pass, the generated value is correct, but appears to be being assigned to all keys in the new array instead of just the new key/value pair. It's almost like the generated value is in fact a reference, but I can't see why.

The code is super-simple:

foreach ($availLangs as $lang) {
$this->data['langLinks'][$lang] = $this->breadcrumbs->getCrumbs($this->location, $lang);
// a var_dump here shows that on each pass, the return value is indeed different.
//But a var_dump of the over-all array shows that each member of $this->data['langLinks'] has been set to the same return value.

//For comparison, this does what is expected i.e. each member of the new array is different.
foreach ($availLangs as $lang) {
$this->data['langLinks'][$lang] = $lang;

I'm battering my head here, and would really appreciate any comments!
2:58 pm on July 17, 2010 (gmt 0)

Senior Member from KZ 

WebmasterWorld Senior Member lammert is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 10, 2005
posts: 2886
votes: 1

Is there a pattern which you can spot? I.e. is the assigned value of all array members the value of the first, the last or a random occurrence of the call to getCrumbs() ?
4:31 pm on July 17, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 13, 2003
votes: 0

Hi, thanks for the reply!

The assigned value is always the value of the last call to getCrumbs().
If I do a var_dump of the whole array on each iteration, i.e. watch the array grow, I can see that all array members are getting the value of the most recent call to getCrumbs().

['en'] = "en_link"

['en'] = "es_link"
['es'] = "es_link"

['en'] = "fr_link"
['es'] = "fr_link"
['fr'] = "fr_link"

It's like the right-hand side is referencing a value, rather than actually being a value.