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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:05:34+00:00 2026-06-18T10:05:34+00:00

Possible Duplicate: How to create an array from a CSV file using PHP and

  • 0

Possible Duplicate:
How to create an array from a CSV file using PHP and the fgetcsv function

I have a file that contains multiple rows of comma delimited values. A sample of the file is:

1359739665,511251515,115151515,start,1277771750,,2215812581258,31.55.115.12,0,0
1359739665,511251515,115151515,restore,1277771750,,2215812581258,31.55.115.12,0,0
1359739665,511251515,115151515,restore,1277771750,,2215812581258,31.55.115.12,0,0
1359739665,511251515,115151515,end,1277771750,,2215812581258,31.55.115.12,0,0

I basically need to iterate through each row. At the moment, this is my code:

$file = "log.txt";
$contents = file_get_contents($file);
$fields = explode(',', $contents);

foreach ($fields as $row)
    {
       echo $row;
    }

This works fine for outputting the entire file. But my question is how do I iterate one row at a time. Each row essentially ends without the comma but I’m not sure how to force a break there. What I’d like to do is this (even though code is incorrect):

foreach ($fields as $row)  //need $row to equal each row of the log
  {
      echo $row[0];  // will output 1359739665
      echo $row[1];  // will output 511251515
      etc...
  }

Thank you!

  • 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-18T10:05:35+00:00Added an answer on June 18, 2026 at 10:05 am

    You should use either str_getcsv() or fgetcsv() to read CSV data.

    An example using fgetcsv():

    if (($handle = fopen("log.txt", "r")) !== FALSE) {
        while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
            echo $row[0] . "\n"; // Will output  data contained in the first column
        }
        fclose($handle);
    }
    

    An example using str_getcsv():

    $file = "log.txt";
    $contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    $csvRows = array_map('str_getcsv', $contents);
    
    foreach($csvRows as $row) {
        echo $row[0] . "\n"; // Will output  data contained in the first column
    }
    

    Using explode() to manually parse CSV data might cause problems when your CSV fields may contain commas within their values or when the file endings of the CSV file isn’t consistent. It is best to use the specialized functions described above as they are tailored for this specific task.

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

Sidebar

Related Questions

Possible Duplicate: How to create comma separated list from array in PHP? I have
Possible Duplicate: How to create comma separated list from array in PHP? My array
Possible Duplicate: Create an Array of the Last 30 Days Using PHP I am
Possible Duplicate: Create a CSV File for a user in PHP I am looking
Possible Duplicate: Is there a PHP function to remove any/all key/value pairs that have
Possible Duplicate: How to create comma separated list from array in PHP? Given this
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: Creating a byte array from a stream I'm trying to create text
Possible Duplicate: Generating a JavaScript array from a PHP array I just want to
Possible Duplicate: How to create ArrayList (ArrayList<T>) from array (T[]) in Java How to

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.