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
$data[3] = str_replace($data[3],'500');. Why don't you just replace the value like this:$data[3] = 500;?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.foreach ($toBoot as $line) { fputcsv($file, $line); }