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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:26:15+00:00 2026-05-30T05:26:15+00:00

I want to skip some records in a foreach loop. For example, there are

  • 0

I want to skip some records in a foreach loop.

For example, there are 68 records in the loop. How can I skip 20 records and start from record #21?

  • 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-30T05:26:17+00:00Added an answer on May 30, 2026 at 5:26 am

    Five solutions come to mind:

    Double addressing via array_keys

    The problem with for loops is that the keys may be strings or not continues numbers therefore you must use “double addressing” (or “table lookup”, call it whatever you want) and access the array via an array of it’s keys.

    // Initialize 25 items
    $array = range( 1, 25, 1);
    
    // You need to get array keys because it may be associative array
    // Or it it will contain keys 0,1,2,5,6...
    // If you have indexes staring from zero and continuous (eg. from db->fetch_all)
    // you can just omit this
    $keys = array_keys($array);
    for( $i = 21; $i < 25; $i++){
        echo $array[ $keys[ $i]] . "\n";
        // echo $array[$i] . "\n"; // with continuous numeric keys
    }
    

    Skipping records with foreach

    I don’t believe that this is a good way to do this (except the case that you have LARGE arrays and slicing it or generating array of keys would use large amount of memory, which 68 is definitively not), but maybe it’ll work: 🙂

    $i = 0;
    foreach( $array as $key => $item){
        if( $i++ < 21){
            continue;
        }
        echo $item . "\n";
    }
    

    Using array slice to get sub part or array

    Just get piece of array and use it in normal foreach loop.

    $sub = array_slice( $array, 21, null, true);
    foreach( $sub as $key => $item){
        echo $item . "\n";
    }
    

    Using next()

    If you could set up internal array pointer to 21 (let’s say in previous foreach loop with break inside, $array[21] doesn’t work, I’ve checked :P) you could do this (won’t work if data in array === false):

    while( ($row = next( $array)) !== false){
      echo $row;
    }
    

    btw: I like hakre’s answer most.


    Using ArrayIterator

    Probably studying documentation is the best comment for this one.

    // Initialize array iterator
    $obj = new ArrayIterator( $array);
    $obj->seek(21); // Set to right position
    while( $obj->valid()){ // Whether we do have valid offset right now
        echo $obj->current() . "\n";
        $obj->next(); // Switch to next object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some easy way to detect it? I want to skip some code
How do we exclude files which follows some pattern like I want to skip
PHP strip_tags use a whitelist for skip some tags that you don't want were
Want to do this: (EDIT: bad sample code, ignore and skip below) struct RECORD
I want to get a Stream from some arbitrary position in an existing file,
I want to skip to the first line that contains include. <> until /include/;
Using Crystal Report 8.5 How to skip blank pages? I want to skip blank
SKIP TO UPDATE 3 I want to enable the Seam debug page on Weblogic
want to have a Hyperlink-Button in a gridView in which I can display a
I am reading a text file and processing some records, a relevant sample of

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.