homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Assigning return values in an array
What am I missing here?

10+ Year Member

Msg#: 4172048 posted 12:27 pm on Jul 17, 2010 (gmt 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!



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

Msg#: 4172048 posted 2:58 pm on Jul 17, 2010 (gmt 0)

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


10+ Year Member

Msg#: 4172048 posted 4:31 pm on Jul 17, 2010 (gmt 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved