PHP read in specific csv file column as an array

17,963

Solution 1

fgetcsv() only reads a single line of the file at a time. You'll have to read the file in a loop to get it all:

$data = array();
while($row = fgetcsv($file)) {
   $data[] = $row;
}

The heading you can skip by doing an fgetcsv once outside the loop, to read/trash the header values. And if you only want the second column, you can do:

   $data[] = $row[1];

However, since you've got data in there, maybe it might be useful to keep it, plus key your new array with the ID values in the csv, so you could also have:

   $data[$row[0]] = $row[1];

and then your nice shiny new array will pretty much exactly match what's in the csv, but as an array keyed by the ID field.

Solution 2

$csv = array_map("str_getcsv", file("data.csv", "r")); 
$header = array_shift($csv); 
// Seperate the header from data

$col = array_search("Value", $header); 
 foreach ($csv as $row) {      
 $array[] = $row[$col]; 
}
// Iterate through data set, creating array from Value column

Solution 3

$header = fgetcsv($h);
$rows = array();
while ($row = fgetcsv($h)) {
    $rows []= array_combine($header, $row);
}

Solution 4

$fp = fopen($filePath, "r+");
$header = fgetcsv($fp);
while ($members = fgetcsv($fp)) {
    $i = 0;
    foreach ($members as $mem) {
        $membersArray[ $i ][ ] = $mem;
        $i++;
    }
}

$newArray = array_combine($header, array_map("array_filter",$membersArray));
Share:
17,963
Admin
Author by

Admin

Updated on June 24, 2022

Comments

  • Admin
    Admin almost 2 years

    I am new to PHP and would like to be able to read in a csv file which has two columns, one is the number (kind of like a ID) then the other holds a integer value. I have looked up the fgetcsv function but I have not been able to find a way to read a specific column from the csv file.

    I would like to get all the values from the second column only, without the heading.

    Any way of doing this?

    This is what I have so far:

    $file = fopen('data.csv', 'r');
    $line = fgetcsv($file);
    

    And this is some sample data from the csv file:

    ID,Value
    1,243.00
    2,243.00
    3,243.00
    4,243.00
    5,123.11
    6,243.00
    7,180.00
    8,55.00
    9,243.00
    

    Any help would be appreciated.

    Thanks.

  • Admin
    Admin over 11 years
    This doesn't seem to work. I have initiated the array, then done the fopen like in my post, then the loop that you have mentioned but using row[1] but it does not run. Any ideas or am I doing something stupid? Thanks for your reply.
  • Marc B
    Marc B over 11 years
    you get nothing at all in $data, or a bunch of blanks? Try doing a var_dump($row) inside the loop, and make sure that the file is properly opened (fopen will return a boolean false on failure, not a file handle).
  • Admin
    Admin over 11 years
    Did the var_dump($row[1]) and it prints the whole of that column not just the single value? Here is some of what it prints: string(6) "243.00" string(6) "243.00". What I need now is all those values in a separate array?
  • Marc B
    Marc B over 11 years
    That'd be weird if it prints a whole column of volumes - fgetcsv only reads one LINE of data. better make sure that the input file's line terminators (\r, \n, etc...) are correcct.e .g. reading a windows text file on a unix machine). check php.net/fgetcsv to specify which chars to use for all that.
  • Admin
    Admin over 11 years
    Okay thanks. I am running this on a MacBook Pro using MAMP so could that be a reason? Also if it is just reading the second column using $row[1], the values in the csv file do not have a comma after. Could that be another reason?
  • Marc B
    Marc B over 11 years
    yes, fgetcsv defaults to using a comma as a separator, so looks like your input file is "wrong" on all sorts of levels. you'll have to read the man page and customize the field separator and line ending char options to match the file.
  • Admin
    Admin over 11 years
    Okay thanks. I was supplied the file as it is kind of a mini test so they will use the file as default for everyone. Probably a little thing they have done to see how we get around it. Thanks for your help, will have to have a look at the documentation.
  • Spudley
    Spudley over 11 years
    your first line just does fgetcsv() the hard way.
  • Admin
    Admin over 11 years
    I have it all working now by reading one line however when I put the line $values[$amountRecords] = $row[1]; to add the value to the values array it doesn't work, comment it out it does. Have any idea why that line will not add the value to the array. I can get it to print each separate value but not add? Looked up online and it says exactly what I have done. Any ideas? Thanks.
  • cHao
    cHao over 11 years
    @Spudley: "The hard way" gets the whole file with one line of code.
  • ptrcao
    ptrcao about 2 years
    @David Houde why do you use double quotes " instead of single quotes ' around parameter values?