0

I get a large csv file (80,000 lines) using file_get_contents(). This file has fields delimited by ";" and I need to add a new column and populate each row of this column, based on the value contained in each "data_002" rows.

below is an example of the original five-column file that I get every morning, using the file_get_contents() function;

data_01;data_02;data_3;data_04;data_05; 
1234567;mydata3;10.644;allowed;02:44:08
8910111;mydata8;10.669;allowed;01:22:03
1213141;mydata6;10.674;allowed;02:44:05
1516171;mydata2;10.676;allowed;01:21:53
8192021;mydata5;10.669;allowed;02:44:49
2223242;mydata1;10.649;allowed;02:43:53
2627282;mydata7;10.705;allowed;02:43:51
9303132;mydata4;10.629;allowed;02:44:45
3334353;mydata1;10.666;allowed;02:43:52
6373839;mydata7;10.653;allowed;02:43:36
4041424;mydata2;10.679;allowed;02:44:24
4445464;mydata8;10.689;allowed;01:21:48

now I have to add the new column "data_006" to the csv file and populate his fields depending on the values contained in the "data_002" column so I need a function like this


function populate_data_006($valueOfColumnData_02){

   if($valueOfColumnData_02 == "mydata1"){     
       $ret = "apricot";
   } elseif($valueOfColumnData_02 == "mydata2"){       
       $ret = "cherry";
   } elseif($valueOfColumnData_02 == "mydata3"){       
       $ret = "peach";
   } elseif($valueOfColumnData_02 == "mydata4"){       
       $ret = "banana";
   } elseif($valueOfColumnData_02 == "mydata5"){       
       $ret = "apple";
   } elseif($valueOfColumnData_02 == "mydata6"){       
       $ret = "pear";
   } elseif($valueOfColumnData_02 == "mydata7"){       
       $ret = "plum";
   } elseif($valueOfColumnData_02 == "mydata8"){       
       $ret = "melon";
   }
   return $ret;
}

foreach csv_row{
   populate_data_006($value)
} 

and in the end this should be the result


data_01;data_02;data_3;data_04;data_05;data_06
1234567;mydata3;10.644;allowed;02:44:08;peach
8910111;mydata8;10.669;allowed;01:22:03;melon
1213141;mydata6;10.674;allowed;02:44:05;pear
1516171;mydata2;10.676;allowed;01:21:53;cherry
8192021;mydata5;10.669;allowed;02:44:49;apple
2223242;mydata1;10.649;allowed;02:43:53;apricot
2627282;mydata7;10.705;allowed;02:43:51;plum
9303132;mydata4;10.629;allowed;02:44:45;banana
3334353;mydata1;10.666;allowed;02:43:52;apricot
6373839;mydata7;10.653;allowed;02:43:36;plum
4041424;mydata2;10.679;allowed;02:44:24;cherry
4445464;mydata8;10.689;allowed;01:21:48;melon

I tried to make this scheme by entering the data in a mysql DB and trying to manipulate it with a php script, but the result was disastrous, as for each row I ran a query (we are talking about a csv file consisting of more then 80.000 rows) and mysql got overloaded and, after the time limit, the script has stopped working.

So I would like to ask those who have more competence to give me some advice on how to be able to complete this daily operation in the fastest way and without stressing the server. Thank you

Mort
  • 3,170
  • 1
  • 24
  • 38
  • the answer given as solution deals with a different topic. I did not ask how to load the csv file in mysql, my question was related to how to add a new column to the csv file and then add new data in this column by creating them based on the values contained in the second column of the same file. All this has nothing to do with the solution indicated by the person who moderated my question. – Roberto Mottola Mar 31 '22 at 05:51

0 Answers0