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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:35:46+00:00 2026-06-12T13:35:46+00:00

So, I have this: $abc = array(‘a’,’b’,’c’); foreach ($abc as $k => &$a) {

  • 0

So, I have this:

$abc = array('a','b','c');
foreach ($abc as $k => &$a) {
    echo $a;
    if ($k == 1)
        $abc[] = 'd';
}

Work’s as expected, iterating through the foreach 4 times and giving me:

abcd

But now, when I have this:

$myvar = $this->someModel->return_an_array_as_result(); // returns array([0] => array('a' => 'b'))

foreach ($myvar as $myvar_key => &$mv){
    $myvar[] = array('e' => 'f');
    var_dump($myvar);
    if ($myvar_key == 5) die;
}

The foreach only runs once.

Any thoughts on how foreach works when resetting the internal pointer?

  • 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-06-12T13:35:48+00:00Added an answer on June 12, 2026 at 1:35 pm

    I see your point, you can use ArrayObject which would allow append to array while you are still loop through the array

    $myvar = new ArrayObject(array('a' => 'b'));
    $x = 0;
    foreach ( $myvar as $myvar_key => $mv ) {
        $myvar->append(array('e' => 'f'));
        if (($x >= 4))
            break;
        $x ++;
    }
    var_dump($myvar);
    

    Output

    object(ArrayObject)[1]
      public 'a' => string 'b' (length=1)
        array
          'e' => string 'f' (length=1)
        array
          'e' => string 'f' (length=1)
        array
          'e' => string 'f' (length=1)
        array
          'e' => string 'f' (length=1)
        array
          'e' => string 'f' (length=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of file-path strings like this videos/funny/jelloman.wmv videos/funny/bellydance.flv videos/abc.mp4 videos/june.mp4 videos/cleaver.mp4
I have a string array abc I put this in a for each loop.
I have this multi-dimensional array: char marr[][3] = {{abc},{def}}; Now if we encounter the
Consider I have this array: $array = array( 'word1', 'abc', 'abc', 'word2', [other words]
I have this array: var arr = []; arr.push({name:k1, value:abc}); arr.push({name:k2, value:hi}); arr.push({name:k3, value:oa});
I have this in PHP: $data = array(test=>array(abc=>xyz)); I want to append ' data:
I have this array I want to get the Values ABC ,1,2 and so
I have a file that looks something like this: ABC DEF GHI I have
I have this use case of an xml file with input like Input: <abc
I have this type of data: entity, account, month1, month2, ..., month12 abc, 2000.02,

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.