2

Supposing we have a CSV created with the following items splitted by ;

A   B   C   D   E   F
G   H   I   J   K   L
M   N   O   P   Q   R

I'm trying to figure out how can I use the following code

$f = fopen($file, "r");
while ($row = fgetcsv($f,'',';')) {
    if ($row[1] == 'H') {       
        echo ('FOUND ! ');      
    }
}

To open the file and after that for the item H found in the CSV file, I would like to replace the K item with K+replace. How is this done?

I want to use this to conditionally insert values in a million entries CSV, I mean if one element is found to update this item with other value.

I am choosing CSV due to the fact that MySQL taking more than 0.5 seconds to search-find-update/replace per item. (everything done in a loop).

3
  • have you looked at str_ireplace()? Commented Mar 9, 2013 at 15:47
  • Why do this in a loop? If H is a value in a column in a MySQL table then an update query of UPDATE table SET (k column to whatever I want) WHERE H column = 'H' will do it. Commented Mar 9, 2013 at 16:03
  • I am introducing thousands of elements through a FOR loop so basically for each $i I am testing if the item already exist in MySQL DB if it exist I am updating the entry. So do the math 1000000*0,5s = ... Not fiable at all :( This is why I am trying with CSV Commented Mar 9, 2013 at 16:08

1 Answer 1

1

The easiest way to do this is to open the file and read it one line at a time like you have, but then open an "output" file and write to that file one line at a time as you loop through each line in the "source" file. So basically

$f = fopen($file, "r");
$o = fopen($output_file, "a");
while ($row = fgetcsv($f,'',';')) {
  if ($row[1] == 'H') {         
    $row[4] = '??'; // whatever you want to replace K with
     echo ('FOUND ! ');        
  }
  fputcsv($o,$row,';');    
}
Sign up to request clarification or add additional context in comments.

7 Comments

can you elaborate on what "not working" means? I did see one typo in my code... $o had the same $file, should have a different filename $output_file (edited)
yes it's working, just needs an else to complete rest of values ... btw can this be done by using the SEEK of the line to be replaced ?
the problem with using fseek and fwrite is that unless the replacement value is equal or less characters in length, it's going to overwrite things. IOW, it doesn't "bump up" the rest of the data in the file. In order to "bump up" the rest of the data, you will have to grab all the data from the fseek point to EOF and put it in a variable - which considering your file is so big, probably will not work out for you. So the easiest thing to do is just do it one line at a time to a 2nd file like what I've shown.
yes... I understand thank you ! one more thing... do you think that csv method is faster than INSERT INTO mytable on DUPLICATE KEY UPDATE ?
also you're right, i forgot about an else to also write the lines not needing change. Well you don't really need an else for that, see edited code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.