Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using foreach effectively in PHP

Tags:

loops

foreach

php

So I don't think I'm making full use of the foreach loop. Here is how I understand foreach.

  • It goes like foreach(arrayyouwanttoloopthrough as onevalueofthatarray)

  • No counter or incrementing required, it automatically pulls an array, value by value each loop, and for that loop it calls the value whatever is after the "as".

  • Stops once it's done with the array.

  • Should basically replace "for", as long as dealing with an array.

So something I try to do a lot with foreach is modify the array values in the looping array. But in the end I keep finding I have to use a for loop for that type of thing.

So lets say that I have an array (thing1, thing2, thing3, thing4) and I wanted to change it....lets say to all "BLAH", with a number at the end, I'd do

$counter = 0;
foreach($array as $changeval){
$counter++;
$changeval = "BLAH".$counter;
}

I would think that would change it because $changeval should be whatever value it's at for the array, right? But it doesn't. The only way I could find to do that in a] foreach is to set a counter (like above), and use the array with the index of counter. But to do that I'd have to set the counter outside the loop, and it's not even always reliable. For that I'd think it would be better to use a for loop instead of foreach.

So why would you use foreach over for? I think I'm missing something here because foreach has GOT to be able to change values...

Thanks

OH HEY One more thing. Are variables set in loops (like i, or key) accessible outside the loop? If I have 2 foreach loops like

foreach(thing as value)

would I have to make the second one

foreach(thing2 as value2) ]

or else it would have some problems?

like image 588
user1159454 Avatar asked Feb 22 '23 15:02

user1159454


2 Answers

You can use a reference variable instead:

foreach ($array as &$value)
{
    $value = "foo";
}

Now the array is full of foo (note the & before $value).

Normally, the loop variable simply contains a copy of the corresponding array element, but the & (ampersand) tells PHP to make it a reference to the actual array element, rather than just a copy; hence you can modify it.

However, as @Tadeck says below, you should be careful in this case to destroy the reference after the loop has finished, since $value will still point to the final element in the array (so it's possible to accidentally modify it). Do this with unset:

unset($value);

The other option would be to use the $key => $value syntax:

foreach ($array as $key => $value)
{
    $array[$key] = "foo";
}

To answer your second question: yes, they are subsequently accessible outside the loop, which is why it's good practice to use unset when using reference loop variables as in my first example. However, there's no need to use new variable names in subsequent loops, since the old ones will just be overwritten (with no unwanted consequences).

like image 71
Will Vousden Avatar answered Mar 02 '23 19:03

Will Vousden


You want to pass by reference:

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value)
{
    $value = $value * 2;
}

// $arr is now array(2, 4, 6, 8)
like image 32
Ryan Pendleton Avatar answered Mar 02 '23 19:03

Ryan Pendleton