简体   繁体   English

PHP 逐行读取 CSV 文件 - 比较上一行中的数据

[英]PHP reading CSV file line by line - compare data in previous line

I have a script set up that reads uploaded CSV files line by line.我设置了一个脚本,可以逐行读取上传的 CSV 文件。 There are about 15 columns in each CSV file.每个 CSV 文件中大约有 15 列。 The file has up to 8 contact people for each row, and each row has a "case number" that acts as a unique ID for each row.该文件的每一行最多有 8 个联系人,每一行都有一个“案例编号”,作为每一行的唯一 ID。 My script then adds those contact people to a database, line by line for however many lines there are.然后,我的脚本将这些联系人逐行添加到数据库中,无论有多少行。

Now, things have changed outside of my control.现在,事情发生了超出我控制的变化。 Each contact person is entered onto each line, and that case number number may show up on multiple lines now.每个联系人都输入到每一行,并且该案例编号现在可能会显示在多行中。

I'm having trouble on how to pursue this now.我现在在如何追求这个方面遇到了麻烦。 I need to be able uploaded the contacts with the same case number to the same row.我需要能够将具有相同案例编号的联系人上传到同一行。 So I would need to INSERT the initial showing of the contact person, and then UPDATE SET that row each time another contact person is encountered with the same case number within my loop.所以我需要插入联系人的初始显示,然后每次在我的循环中遇到具有相同案例编号的另一个联系人时更新该行。

I don't have any code to show.我没有要显示的代码。 I'm just having trouble with how to approach this logistically.我只是在如何从逻辑上解决这个问题上遇到了麻烦。 I will probably be downvoted, but I'll keep trying things on my end.我可能会被否决,但我会继续尝试。

I ended up inserting all of the data into a table, and then querying that table for the unique identifier, and then populating a different table with that unique identifier and all of the contacts that had that identifier.我最终将所有数据插入到一个表中,然后在该表中查询唯一标识符,然后使用该唯一标识符和所有具有该标识符的联系人填充另一个表。 I was over complicating this from the beginning but it's good to know about INSERT ON DUPLICATE KEY UPDATE我从一开始就把这个复杂化了,但很高兴知道在重复密钥更新上插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM