There is a set of data that needs to be inserted into the database, such as importing a group of data from EXCEL to the MYSQL database. how can we avoid repeated insertion, here, the repetition is not the repetition of a field, but the combination of multiple fields, for example, idnamecontent & nbsp; 1 Three Meals & nbsp; 2 Li Si's question about avoiding repeated data insertion and counting the number of duplicated data
There is a set of data that needs to be inserted into the database, such as importing a set of data from EXCEL to the MYSQL database, how to avoid repeated insertion, here the repetition does not refer to the repetition of a field, it is a combination of multiple fields, for example:
Id name content
1. three meals
2 Li Si
3. drink water
As shown above, if the values of the name and content fields are repeated, for example, if you insert another "Michael, eat", you will be prompted to repeat and count the number of duplicates and the number of inserted data, evaluate the PHP statement.
------ Solution --------------------
PHP code
$ ExcelData = array ('Zhang San', 'Weiwei'), array ('li Si', 'Weiwei'), array ('Zhang San', 'drinking water '), array ('li Si', 'Eat '), // repeat array ('Zhang San', 'drinking water') // repeat); $ mark = array (); // used as the unique identifier foreach ($ excelData as $ key => $ arr) {$ md5Mark = md5 ($ arr [0]. $ arr [1]); if (isset ($ mark [$ md5Mark]) {unset ($ excelData [$ key]);} else {$ mark [$ md5Mark] = 1 ;}// you can operate $ excelData without judgment, of course, you can also directly combine SQL statements in else or write the database p ($ excelData);/* output: array ([0] => Array ([0] => Zhang San [1] => eat) [1] => Array ([0] => Li Si [1] => eat) [2] => Array ([0] => Zhang San [1] => drinking water ))*/