www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to read Specific CSV column data into a PHP variable

  1. #1
    Join Date
    Jan 2007
    Posts
    78

    How to read Specific CSV column data into a PHP variable

    I have a CSV file with column headings;
    Name,email,areacode,phonenumber
    How can I read only the data from col 2 ( the email addresses ) into a variable, without the column headings.
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,157
    Have you looked at fgetcsv() yet?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2003
    Posts
    599
    There is a class available HERE that you can use to output a specific column to either an array of values, or comma separated string.

    Look in the commented out section of the the advanced example for the "$CSV->output_column()" method.

    This should make quick work of extracting values from a CSV in any way you want. To extract a column would look like this:

    PHP Code:
    require('class.csvCRUD.php');
    $data_file 'my_data_file.txt';
    $file_delimiter '|';

    $csv = new csvCRUD($data_file,$file_delimiter);

    //To output col B as an array of values:
    $col_2_values_array $csv->output_column('B','array');

    //Or to output col B as a comma delim string of values:
    $col_2_values_string $csv->output_column('B','string'); 
    If you do not want to use the class, you can dig in and look at the functions that do the work...I like using the class because there are also built in methods to dump results to a table, XML, JSON, etc... You can also "query" a CSV to extract only certain records, or use the built in forms to edit CSV records.
    Last edited by hastx; 09-20-2013 at 11:12 AM.
    We do precision guesswork

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,155
    You either look at NogDog's idea of a CSV function, read in the data and you can ignore the first line or...

    Assuming that you are parsing the file to insert in to a database, you could do something like...
    PHP Code:
    // split at the new line char
    $content explode("\r\n",file_get_contents("filename.ext"));

    // remove the first entry = header
    array_shift($content);

    // make a multi-dimentional array
    foreach( $content as $entry=>$str )
        
    $content[$entry] = explode(","$str);

    // make array to store results
    $emails = array();

    // put emails in to new array
    foreach( $content as $entry )
        
    array_push$entry[1] );

    // house keeping
    unset( $content ); 
    Thats a rather long winded way of doing it, you can use other methods to get the data you want but from an educational point of view its easier to follow the process.

    Without the comments and the house keeping, its a five line routine.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Jan 2007
    Posts
    78
    Thanks guys.
    Somehow, my responses aren't being posted.
    I will definitely look into the class.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles