我有一个看起来像这样的文件:
Papiers peints > 3D et Perspective > 3D
Papiers peints > Carte du monde
Papiers peints > Fleurs
Papiers peints > Fleurs > Coquelicots
Tableaux > Cartes du monde
Tableaux > Fleurs
Tableaux > Fleurs > Coquelicots
然后我将其转换为格式正确的 csv,如下所示:
"Papiers peints","3D et Perspective","3D"
"Papiers peints","Carte du monde",
"Papiers peints","Fleurs",
"Papiers peints","Fleurs","Coquelicots"
"Tableaux","Cartes du monde",
"Tableaux","Fleurs",
"Tableaux","Fleurs","Coquelicots"
我需要的是这些字段中的每一个都有自己的唯一 ID,它必须是一个整数。这就是它的样子
"Papiers peints",101,"3D et Perspective",1001,"3D",10001
"Papiers peints",101,"Carte du monde",1002,,
"Papiers peints",101,"Fleurs",1003,,
"Papiers peints",101,"Fleurs",1003,"Coquelicots",10002
"Tableaux",102,"Cartes du monde",1004,,
"Tableaux",102,"Fleurs",1005,,
"Tableaux",102,"Fleurs",1005,"Coquelicots",10003
名称本身根本不重要,总会有重复的。我可以通过上传到数据库轻松解决这个问题。然后我做:
select distinct COL1
,给他们各自的IDselect COL2, group by COL1, COL2
,给他们各自的IDselect COL3, group by COL1, COL2,COL3
,给他们各自的ID- 根据需要重复多次,这可能很多
如何在 PHP 中执行此操作而无需使用数据库?一个直接的答案会很好,但即使是设计理念也会有很大帮助。
我的文件中最多可以有 10 列,但这里有一个简化的输入数组可以使用:
$new=[0=>['a0','a1','a2','a3'],1=>['b0','b1','b2','b3'],2=>['c0','c1','c2','c3'],3=>['d0','d1','d2','d3'],4=>['e0','e1','e2','e3']];
预期结果:
[
['a0','101','a1','1001','a2','10001','a3','100001'],
['b0','102','b1','1002','b2','10002','b3','100002'],
['c0','103','c1','1003','c2','10003','c3','100003'],
['d0','104','d1','1004','d2','10004','d3','100004'],
['e0','105','e1','1005','e2','10005','e3','100005']
]