Jump to content

Can't understand foreach's array reference to modify values


Slips

Recommended Posts

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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                                                                                                                 
)

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.