Slips Posted November 17, 2010 Share Posted November 17, 2010 Hello, I've been stuck on this code for nearly 3 days and i still am not able to understand this too well. The PHP manual says "Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it." Right , so from what i understand from that,if i want to modify an array inside a foreach , i need to reference the array. This example was given in the PHP manual to demonstrate this reference requirement <?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 ?> But, when i try this code : <?php $array = array('Ibanez'); foreach ($array as $guitar) { $array['guitar'] = $array[0]; unset($array[0]); $array['guitar'] = 'Gibson'; } print_r($array); ?> The output of the array outside the foreach shows the changed array and not the original anymore. So doesn't this mean that the foreach CAN indeed operate on the array itself without the array being referenced? The reason i'm stuck with this code is that i'm trying to use another slightly bigger array which needs to have its keys modified within the foreach statement, but the changes refuse to leave the foreach loop. But when i tested this small piece of code above, changes are indeed getting reflected without any reference. I'm really confused now about how this works. Help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/ Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 Thats becase the reference value isnt tied to the array $example=array(1,2,3,4,5) foreach($example as $idx=>$val) { $example[$idx]+=2; } print_r($example); Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135429 Share on other sites More sharing options...
Slips Posted November 17, 2010 Author Share Posted November 17, 2010 Sorry didn't quite understand what you meant there. The code you provided just proves that you can change array values without writing the code as foreach ($array as $key => &$value). Thats what my code proves aswell, but the question i meant to ask was, why does the manual say that the original array value needs to be referenced with a & in the foreach statement, when clearly we can change original array values without it. Thanks again, for the help. Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135440 Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 But you are using a referance to the original array, how do you not expect it not to change. if u dont want the original to change, make a copy, and work on the copy array Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135499 Share on other sites More sharing options...
AbraCadaver Posted November 17, 2010 Share Posted November 17, 2010 Sorry didn't quite understand what you meant there. The code you provided just proves that you can change array values without writing the code as foreach ($array as $key => &$value). Thats what my code proves aswell, but the question i meant to ask was, why does the manual say that the original array value needs to be referenced with a & in the foreach statement, when clearly we can change original array values without it. Thanks again, for the help. The manual doesn't say "that the original array value needs to be referenced with a & in the foreach statement" in order to change the array or the value.. It says, "As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value." The meaning is this, you can't change the foreach $value of the array using the AS variable. It doesn't mean that you can't modify the value or the original array. Pay attention to $value: $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } print_r($arr); Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 ) $arr = array(1, 2, 3, 4); foreach ($arr as $value) { $value = $value * 2; } print_r($arr); Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135581 Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 in a foreach statement &$val is a reference to the the original, so any change to $val changes the original $val is not referenced, any changes to $val does not alter the original so your logic is in err as this code proves what they mean $arr = array(1,2,3,4,5) foreach($arr as $val) $val+=5; print_r($arr) foreach($arr as &$val) $val+=5; print_r($arr) Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135591 Share on other sites More sharing options...
Slips Posted November 17, 2010 Author Share Posted November 17, 2010 Wow that clears up a lot, thanks a lot guys, this forum is definitely giving my learning a big boost. Quote Link to comment https://forums.phpfreaks.com/topic/218939-cant-understand-foreachs-array-reference-to-modify-values/#findComment-1135603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.