PHP fgetcsv() function and arrays

Discussion in 'PHP' started by pitto, Apr 1, 2008.

  1. #1
    Hi,
    I am new to php and am having a lot of trouble understanding the nature of the array created by fgetcsv(). I have a .csv file that looks like this:

    data 00,data 01
    data 10,data 11
    data 20,data 21
    ...

    I was under the impression that fgetcsv($handle, 1000, ",") where $handle is assigned the value of the name of the above .csv file would turn this .csv into something like this:

    Array ([0][0] => data 00, [0][1] =>data 01, [1][0] =>data 10, [1][1] =>data 11, ... )

    But it seems that all it does is take one line and it doesn't even split that comma separated data into 2 fields in the array. So I get:

    Array ([0] =>data 00,data 01)

    where 'data 00,data 01' is a string.

    What is the sense of setting the delimiter in fgetcsv() to "," if it does nothing with it? I must be doing something wrong but I can't figure out what. Can anyone tell me how to get an array with the structure I want above? I realize I'll need to use a loop to work through each line of the csv file but why isn't fgetcsv() breaking my comma-separated values into 2 fields?
     
    pitto, Apr 1, 2008 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I've just been using this for a data import and I'll summarise my code for you
    $handle = fopen('northland.csv', 'r');
    $nl = "\n";
    //have we got anything to work with?
    if ($handle)  
    {
    	//loop through the file getting one line at a time & process
    	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    	{
    	    $id = $data[6];
    	    $insert = ($row > 1);
    	       
    	    if (empty($data[21])) $membershipname = $data[10] . ' ' . $data[12];
    	    else $membershipname = $data[21];
    	    $membership = $data[6] . $nl .
    	    			'Membership: '. $membershipname . $nl .
    	    			'Receipt: ' . $data[3] . $nl .
    	    			'Work Phone: ' . $data[15] . $nl .
    	    			'Address1: ' . $data[17] . $nl .
    	    			'Address2: ' . $data[18] . $nl .
    	    			'Address3: ' . $data[19]  ;
    	    			
    	    $individual = 'First: ' . $data[10] . $nl .
    	    			'Last: ' . $data[12] . $nl .
    	    			'Associate: ' . $data[11]. $nl .
    	    			'Email: ' . $data[22]. $nl .
    	    			'Home Phone: ' . $data[14] . $nl .
    	    			'Mobile: ' . $data[16];
    	    	
    	    $financial = 'Paid: ' . $data[1] . $nl . 
    	    			'Expires: ' . $data[4] . $nl .
    	    			'Amount: '.$data[2] . $nl . $membershiptype;
    	    $expires = builddate($data[4]);
    	    $starts = builddate($data[1]);
    	    $paid = str_replace('$','',$data[2]);
    	    
    	    $nzpifcards = 'Expires: ' . $data[5];
    	    $cardexpires = builddate($data[5]);
    	    
    	    $calls = 'Invoice Sent: ' . $data[7] ;
    	    }			
    	        				
    	    $role = $data[13];
    	    
    	    echo "<tr>
    	    	<td>Record <b>{$row}</b> <small>{$num}</small></td>
    	    	<td>{$membership}</td>
    	    	<td>{$individual}</td>
    	    	<td>{$financial}</td>
    	    	<td>{$nzpifcards}</td>
    	    	<td>{$calls}</td>
    	    	<td>{$role}</td>
    	    </tr>\n";
    	    $row++;
    	}
    }
    fclose($handle);
    PHP:
    and remember to check www.php.net/fgetcsv
     
    sarahk, Apr 2, 2008 IP