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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:13:32+00:00 2026-06-03T09:13:32+00:00

$animals = array(‘cat’, ‘dog’, ‘horse’, ‘elephant’); foreach($animals as $animal) { var_dump($animal); next($animals); } The

  • 0
$animals = array('cat', 'dog', 'horse', 'elephant');
foreach($animals as $animal)
{
   var_dump($animal);
   next($animals);
}

The code above outputs: cat, dog, horse, elephant. I thought the next function should move the internal pointer of $animals and, thus, I should be getting this output instead: cat, horse.

How do I make the internal pointer of $animals move forward (and backwards) such that it is affected in the foreach?

EDIT 1:

From the manual:

As foreach relies on the internal array pointer changing it within the
loop may lead to unexpected behavior.

Nonetheless, I think this is what I need to do.

EDIT 2:

Per “Your Common Sense”‘s link, I will provide a more detailed explanation of my problem. Here’s some psuedo code:

array $foos;

start loop of $foos
 - do thing #1
 - do thing #2
 - do thing #3
 - keep doing thing #3 while the current value of $foos in the loop meets a certain criteria
loop

When execution returns to the start of the loop, it should continue from the last array accessed by #3. Note that the array is associative, thus a for ($i = 0 ... approach won’t work.

  • 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-03T09:13:33+00:00Added an answer on June 3, 2026 at 9:13 am

    You can just create an ArrayIterator­Docs which is seekable­Docs.

    As it is an iterator, you can change the position while you iterate over it, some rudimentary example:

    foreach ($iterator as $current) {
        $iterator->next();
    }
    

    It should offer everything you need out of the box. If not, you could encapsulate your needs into an Iterator­Docs on your own as well.

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

Sidebar

Related Questions

I have this following code: foreach ($animals as $animal) { $animal = getOffSpring($animal); }
Using ARC... NSArray *array = [NSArray arrayWithObjects:@dog, @cat, @mouse, nil]; for(NSString *animal in array)
I have a string array: string[] Animals = {Cat, Dog, Fish}; I then want
I have an array with images of animals, another array with the corresponding animal's
Hello you naughty code animals.. I have tried to get this code to work,
I have a JSON array that looks like this: ['Monkey','Cheetah','Elephant','Lizard','Spider'] I also have a
I am fetching an array of animal names, types, age, etc. from a MySQL
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have an array that sets a number for each animal. I want to
In my values.xml file, I have an array, like this; <string-array name=animals-array> <item>Cow</item> <item>Pig</item>

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.