loutka Posted September 25, 2008 Share Posted September 25, 2008 Hi guys! 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: <?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: <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 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! Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/ Share on other sites More sharing options...
kenrbnsn Posted September 25, 2008 Share Posted September 25, 2008 Using the "&" only works with PHP5. Which version of PHP are you running? Ken Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650506 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 You could just do this: <?php $arr = array(1, 2, 3, 4); foreach ($arr as $k=>$value) { $arr[$k] = $value * 2; } // $arr is now array(2, 4, 6, unset($value); // break the reference with the last element ?> Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650510 Share on other sites More sharing options...
loutka Posted September 25, 2008 Author Share Posted September 25, 2008 I know, sorry I didn't posted version staight away: PHP version: 5.2.6 Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650513 Share on other sites More sharing options...
loutka Posted September 25, 2008 Author Share Posted September 25, 2008 You could just do this: <?php $arr = array(1, 2, 3, 4); foreach ($arr as $k=>$value) { $arr[$k] = $value * 2; } // $arr is now array(2, 4, 6, unset($value); // break the reference with the last element ?> I'm using an array within an array and need to change all values with particular key ($menu_item['path']) in the inner array. Does it make sense to you? Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650522 Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 Sure. It will still work. <?php foreach ($items as $k=>$menu_item){ $items[$k]['path'] = drupal_get_path_alias( 'taxonomy/term/' . end(explode('/',$menu_item['path'])) ); } unset ($menu_item); ?> Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650526 Share on other sites More sharing options...
discomatt Posted September 25, 2008 Share Posted September 25, 2008 Oops, mistake made. Let me re-read. Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650577 Share on other sites More sharing options...
discomatt Posted September 25, 2008 Share Posted September 25, 2008 Odd, use phpinfo() to verify it's executing with PHP5 <pre><?php $array = array( 'foo', 'bar', 'hello', 'world' ); foreach( $array as &$val ) { $val .= ' modified'; } unset( $val ); print_r( $array ); ?></pre> Outputs Array ( [0] => foo modified [1] => bar modified [2] => hello modified [3] => world modified ) for me. PHP 5.2.6, Apache 2.2.8 Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650594 Share on other sites More sharing options...
loutka Posted September 25, 2008 Author Share Posted September 25, 2008 Thank you all guys, really appreceate! It is working now... Thank you kenrbnsn: My fault, I've checked it just in cPanel Thank you F1FAN: Of course I used it this way many times... I think I had one of those days when you look in your code and nothing works Thank you DISCOMATT: phpinfo() => PHP Version 4.4.9 But hosting provider claims we have PHP 5.2.6 installed... Grrr! Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650782 Share on other sites More sharing options...
discomatt Posted September 26, 2008 Share Posted September 26, 2008 Read the support/FAQ pages. Some hosts require .htaccess directives or certain extensions to parse with PHP5 (.php5) Link to comment https://forums.phpfreaks.com/topic/125804-passing-reference-to-array-in-foreach-produce-error/#findComment-650906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.