Passing reference to array in "foreach" produces error

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
loutka
Forum Newbie
Posts: 2
Joined: Thu Sep 25, 2008 10:58 am

Passing reference to array in "foreach" produces error

Post by loutka »

Hello,
I'm trying to access array through foreach and change values for particular key.

I'm able to access array without reference, but when I add & to create reference I receive:
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /home/.../modules/taxonomy_menu/taxonomy_menu.inc on line 185
My code:

Code: Select all

 
<?php
foreach ($items as &$menu_item){       
    $menu_item['path'] = drupal_get_path_alias( 'taxonomy/term/' . end(explode('/',$menu_item['path'])) );               
  }
unset ($menu_item);
?>
 
Regarding to http://uk3.php.net/foreach this should work:

Code: Select all

 
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
 
I'm really confused and can't figure it out... ???

Any way around? Can I access array value directly (from within fereach) and change it?

Could anyone help me please?
Thanks!
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Passing reference to array in "foreach" produces error

Post by Darkzaelus »

Code: Select all

 
foreach ($arr as $key => $value) {
    $value = $value * 2;
}
 
Using & passes a reference. As you have no variable called $value, it will throw an error.


Cheers,

Darkzaelus.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Passing reference to array in "foreach" produces error

Post by Stryks »

Actually ... at first glance that reference method should work. I've used it like that before.

Anyhow, as a workaround ...

Code: Select all

foreach($items as $key=>$menu_item) {
   $items[$key]['path'] = drupal_get_path_alias( 'taxonomy/term/' . end(explode('/',$menu_item['path'])) );
}
... or ... something like that. Cant test it here, so cant be more positive of the outcome.

Cheers
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Passing reference to array in "foreach" produces error

Post by Stryks »

Also, just note that the reference method only works with PHP 5 and later.

Perhaps you are running a previous version of PHP?
loutka
Forum Newbie
Posts: 2
Joined: Thu Sep 25, 2008 10:58 am

Re: Passing reference to array in "foreach" produces error

Post by loutka »

Thank you guys!

All solved now...

My hosting provider claims our PHP version is 5, but phpinfo() says PHP version 4.2.6.
- that's why referencing didn't work

So I've used way around by using key.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing reference to array in "foreach" produces error

Post by VladSun »

loutka wrote:My hosting provider claims our PHP version is 5, but phpinfo() says PHP version 4.2.6.
Try to rename your files from *.php to *.php5 ;)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply