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 638833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:46:32+00:00 2026-05-13T20:46:32+00:00

i want to have a foreach loop where the initial array is changed inside

  • 0

i want to have a foreach loop where the initial array is changed inside the loop.

eg.

$array = array('red', 'blue');
foreach($array as $key => $value) {
    $array[] = 'white';
    echo $value . '<br />';
}

in this loop the loop will print out red and blue although i add another element inside the loop.

is there any way to change the initial array inside the loop so new elements will be added and the foreach will use the new array whatever is changed?

i need this kind of logic for a specific task:

i will have a if statement that search for a link. if that link exists, it is added to the array. the link content will be fetched to be examined if it contains another link. if so, this link is added, and the content will be fetched, so on so forth.. when no link is further founded, the foreach loop will exit

  • 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-13T20:46:33+00:00Added an answer on May 13, 2026 at 8:46 pm

    I don’t think this is possible with a foreach loop, at least the way you wrote it : doesn’t seem to just be the way foreach works ; quoting the manual page of foreach :

    Note: Unless the array is referenced, foreach operates on a copy
    of the specified array and not the
    array itself.

    **Edit : after thinking a bit about that note, it is actually possible, and here’s the solution :**

    The note says "Unless the array is referenced" ; which means this portion of code should work :

    $i = 0;
    $array = array('red', 'blue');
    foreach($array as $key => & $value) {
        $array[] = 'white';
        echo $value . '<br />';
        if ($i++ >= 5) {
            break;   // security measure to ensure non-endless loop
        }
    }
    

    Note the & before $value.

    And it actually displays :

    red
    blue
    white
    white
    white
    white
    

    Which means adding that & is actually the solution you were looking for, to modify the array from inside the foreach loop 😉

    **Edit : and here is the solution I proposed before thinking about that note :**

    You could do that using a while loop, doing a bit more work "by hand" ; for instance :

    $i = 0;
    
    $array = array('red', 'blue');
    
    $value = reset($array);
    while ($value) {
        $array[] = 'white';
        echo $value . '<br />';
        if ($i++ >= 5) {
            break;   // security measure to ensure non-endless loop
        }
        $value = next($array);
    }
    

    Will get you this output :

    red
    blue
    white
    white
    white
    white
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this foreach loop - and I want to modify the array
Suppose, I have very large foreach loop. I want put this loop into try
i have two foreach loop to display some data, but i want to use
I have the following code: foreach (var control in this.Controls) { } I want
I have code that I want to look like this: List<Type> Os; ... foreach
I have a foreach loop where I want to unset an item from an
I have a while and foreach loop that I want to use to print
I have placed a execute sql task inside the foreach loop container , where
I have a foreach loop in php to iterate an associative array. Within the
I have a foreach loop for 'n' number of cubicles. I want to display

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.