1

I have a csv file that looks something like this (there are many more rows):

Jim,[email protected],8882,456
Bob,[email protected],8882,343

What I want to do is to change all the values in the fourth column,456,343 to 500.

I'm new to php and am not sure how to do this. I have tried

<?php
$file = fopen('myfile.csv', 'r+');
$toBoot = array();
while ($data = fgetcsv($file)) {
    echo $data[3];
    $data[3] = str_replace($data[3],'500');
    array_push($toBoot, $data);


}
//print_r($toBoot);
echo $toBoot[0][3];
fputcsv($file, $toBoot);
fclose($file)

?>

But it prints

Jim,[email protected],8882,456
Bob,[email protected],8882,343
Array,Array

not

 Jim,[email protected],8882,500
 Bob,[email protected],8882,500

I've looked at this post, PHP replace data only in one column of csv but it doesn't seem to work.

Any help appreciated. Thanks

10
  • 3
    This line looks wrong: $data[3] = str_replace($data[3],'500');. Why don't you just replace the value like this: $data[3] = 500; ? Commented Jul 16, 2018 at 20:45
  • 1
    Can you not do that in Excel, it would be so much simpler Commented Jul 16, 2018 at 20:45
  • @Karol Samborski That just appends Array,Array,Array to the end of the file Commented Jul 16, 2018 at 20:49
  • If that is your actual code, and the actual output, then I am struck by the fact that echo $data[3]; seems to be outputting "Jim,[email protected],8882,456" and "Bob,[email protected],8882,343". I would be quite curious to see the actual source file. Commented Jul 16, 2018 at 20:49
  • 1
    Purpose of fputcsv is to append a csv line to a file. So you need some loop: foreach ($toBoot as $line) { fputcsv($file, $line); } Commented Jul 16, 2018 at 20:51

3 Answers 3

1

You can use preg_replace and replace all values at once and not loop each line of the CSV file.

Two lines of code is all that is needed.

$csv = file_get_contents($path);
file_put_contents($path, preg_replace("/(.*),\d+/", "$1,500", $csv));

Where $path is the path and to the CSV file.

You can see it in action here: https://3v4l.org/Mc3Pm

Sign up to request clarification or add additional context in comments.

Comments

1

A quick and dirty way to way to solve your problem would be:

foreach (file("old_file.csv") as $line)
{
    $new_line = preg_replace('/^(.*),[\d]+/', "$1,500", $line);
    file_put_contents("new_file.csv", $new_line, FILE_APPEND);
}

Comments

1

To change one field of the CSV, just assign to that array element, you don't need to use any kind of replace function.

$data[3] = "500";

fputcsv() is used to write one line to a CSV file, not the entire file at once. You need to call it in a loop. You also need to go back to the beginning of the file and remove the old contents.

fseek($file, 0);
ftruncate($file, 0);
foreach ($toBoot as $row) {
    fputcsv($file, $row);
}

Comments

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.