Passing reference to array in "foreach" produces error
Posted: Thu Sep 25, 2008 11:01 am
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:
Regarding to http://uk3.php.net/foreach this should work:
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!
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:
My code:Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /home/.../modules/taxonomy_menu/taxonomy_menu.inc on line 185
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);
?>
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
?>
Any way around? Can I access array value directly (from within fereach) and change it?
Could anyone help me please?
Thanks!