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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:35:50+00:00 2026-06-14T19:35:50+00:00

I am currently using fgetcsv in php to get rows in CSV file. For

  • 0

I am currently using fgetcsv in php to get rows in CSV file. For each row, I need it’s 3 previous rows and 3 next rows for some requirement.

while(($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
    //$data is current row data
    //I need previous rows and next rows data
}

I don’t know how to try this, as each while iterate will be having the current row’s information.

Is there anything we can achieve this within the fgetcsv loop? I am also open for other alternatives for this problem.

  • 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-14T19:35:51+00:00Added an answer on June 14, 2026 at 7:35 pm

    You have to store these rows yourself.

    I would do something like this:

    $rows = array();
    
    $index = 0;
    
    while(($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
        $rows[] = $data;
    
        if($index >= 6) {
            $currentIndex = $index - 4;
            $currentRow = $rows[$currentIndex];
            //Do something with $rows[$currentRowIndex +- 3];    
        }
        $index++;
    }
    

    If the data is too big to keep it all stored in an array, you could also opt to only store at most 7 rows, and operate on row 4:

    $rows = array();
    
    while(($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
        $rows[] = $data;
        if(count($rows) == 7) {
            $currentRow = $rows[4];
            //Do something with $rows[0-6];
            array_shift($rows);    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am parsing a CSV file using fgetcsv, specifically using $line_of_text. I want to
Im currently using JSP and need to get the a cell in a table.
iam currently using NSURLConnection class to hit my server and get some data from
I'm having a problem using a CSV file uploaded via a PHP form. Here
I currently using android NDK to write some native code in C. I have
so currently using ajax with JQuery .post to return a form populate with some
Im currently using PHP to fetch results from a mysql db. Im also displaying
Im currently using C# to pass some objects to powershell and executing a method
I am using the current code to read a csv file and add it
Currently using a MySQL database Wanting to insert a new row into a table

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.