Csv class assignment

I have 12 columns with 500 names/records in CSV but it returns all 5K data. How can I return it in a list form with 500 records with 12 info/values in it in PHP? (I won’t use MySQL because our teacher doesn’t want us to do it for now)

I’m not clear what your asking…

Are you asking about opening/reading a .csv file using PHP?

Its practically done for you… no? Its already a comma separated value file…

Use fopen to open the file, and fgetcsv to parse it into a PHP array.

1 Like

the file actually looks like that… it’s just about opening the csv file and then returning them into a list form of output with 500 names in it including each of their information… so the first record is James Butt with the rest of his other info.


This is my code based on what I see on the internet…

<!DOCTYPE html>

<html lang="en">


    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Assignment #21 - CSV</title>





$filename = 'us-500.csv';

// The nested array to hold all the arrays

$the_big_array = []; 

// Open the file for reading

if (($h = fopen("{$filename}", "r")) !== FALSE) 


  // Each line in the file is converted into an individual array that we call $data

  // The items of the array are comma separated

  while (($data = fgetcsv($h, 1000, ",")) !== FALSE) 


    // Each individual array is being pushed into the nested array

    $the_big_array[] = $data;   


  // Close the file



// Display the code in a readable format

echo "<pre>";


echo "</pre>";





My output only returns one array with 5K+ values in it.

can I have a sample code?

The new-lines in the file are not being recognized, so, you are getting all the lines in the file as a single line of data. See the following from the documentation -

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

Your code is already using those functions. Are you not actually reading and learning from what you have done?

phpdr is right; your problem is the line ending characters in your csv file don’t match the ones your system expects. Line endings are an example of a non printing character that mean something special; the most common ones are new lines and tabs.

Unfortunately, different computer systems disagree on how a new line should be stored in a file. If your file uses a line ending character that your computer doesn’t use, you’ll get problems like the one you’ve found. The auto_detect_line_endings setting mentioned by phpdr might help you here; you can enable it using ini_set at the top of your script:

ini_set("auto_detect_line_endings", "1");

ok. thank you but how do I output the user’s info with their details?

I am not sure if my code is right:

foreach($the_big_array as $key => $value){

    echo $value;


I want the output to be like that


Stop posting images… and post an example files of your data.

You have already been told the issue… (code isnt seeing the end of each line)

If you look at the contents of $the_big_array, you should see that the first subarray contains your column headings. You can take that row out of your array (look at array_shift to help with this) and use it to print out the right headings for each of your results.

Sponsor our Newsletter | Privacy Policy | Terms of Service