Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:14:41+00:00 2026-05-29T04:14:41+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:14:42+00:00Added an answer on May 29, 2026 at 4:14 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
I don't like Jackson. I want to use ajax but with Google Gson. So
Don't think there's much to say, here's my code for (int i = 0;
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't know what's wrong here, when I run the application it says Specified method
Don't think my virtualhost is working correctly. This is what I have inside of
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't understand, if Data.Map is and [] is. I found this out while wondering
I don't know if this has been asked before, but what i'd like to
Don't ask me why but i can't use this method because I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.