I seem to be having some trouble with the fgetcsv function. In my code I am trying to create a single row array with each tab separated value as a separate element in the array. Instead of returning just the first value, $data[0] is returning the entire line. The problem seems to be that I don't have the right delimiter in fgetcsv, "/t" is not working. Any ideas how to fix this?

Here is my code,

#!/usr/bin/env php


require ("../includes/functions.php");

//argument argv[1] == the relative path 

 if (!file_exists($argv[1]))
     print("The file you chose does not exist.");


    print("The file you chose is not readable");
     $handle=fopen($argv[1], "r");   

     $data = fgetcsv($handle, "\t");



fgetcsv reads in the whole line by design: http://php.net/manual/en/function.fgetcsv.php

this is probably what you had in mind: http://php.net/explode

  • I am not sure I understand, what is the point of putting in the string delimiter \t in getcsv if it doesn't mark each element in the array. May 14 '15 at 9:16
  • It is clear to me now that getcsv will only ever read a single line. Thanks! I have re-edited my question specifically for the question of the delimiter. May 14 '15 at 10:15
  • incidentally, if you are doing pset 7, cs50 finance and not pset 8 as you have in your heading, then you can open the file as txt to see what the delimiter is
    – ronga
    May 15 '15 at 5:52
  • Ok so finally I took your advice and used explode and I got it to work! Still not sure why the other way is not working. May 15 '15 at 8:48
  • the other way works, but you need to know the correct delimiter as i indicated above. hint: what does csv stand for?
    – ronga
    May 16 '15 at 5:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .