Welcome to WebmasterWorld Guest from 54.161.255.61

Forum Moderators: coopster & jatar k

Assigning return values in an array

What am I missing here?

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

10+ Year Member



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 Jul 17, 2010 (gmt 0)

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



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 Jul 17, 2010 (gmt 0)

10+ Year Member



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().

I.e.
['en'] = "en_link"

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

Then
['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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month