www.webdeveloper.com
Results 1 to 3 of 3

Thread: Edit a line in csv file

  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Question Edit a line in csv file

    Hi,

    How can I edit a specific line in my csv-file?

    my csv looks like this

    key1, value1, value2, value3, value4
    key2, value5, value6, value7, value8
    ...

    What I want is to edit the line with the specific key(for example key1). And I want to change value4 by value10.

    I was already trying to read the csv with this function:

    $a = array();
    $f = fopen("gebruikers.csv", 'r');
    while ($line = fgetcsv($f, 1000, ","))
    {
    $key = array_shift($line);
    $a[$key] = $line;
    }
    fclose($f);


    and so I get an array like this:

    Array ( [key1] => Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4) ) this array has the name $a
    ...

    Next I get the line by $b = $a[key1] and change it by the value $b[3] = $value10.
    after this I put it back in the big array $a[key1] = $b.

    Now the big array $a is edited correctly, but how do I write it back to my csv-file?

    (sorry for my english )

  2. #2
    Join Date
    Apr 2012
    Posts
    38
    I'm not very familiar with writing to CSV files but it seems that PHP.net has a good function to do this:

    http://php.net/manual/en/function.fputcsv.php

  3. #3
    Join Date
    May 2003
    Posts
    599
    Here is a class that deals with CSV files as an array of data and allows editing of the fields by a web form. I think it will do exactly what you are looking for:
    http://www.phpclasses.org/package/78...CSV-files.html

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