为多维数组中的所有列值生成唯一 ID

Generate unique ids for all column values in a multi-dimensional array

我有一个如下所示的文件:

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

名字本身并不重要,总是会有重复的。 我可以通过上传到数据库轻松解决这个问题。 然后我做:

如何在 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']
]

如果这是我的项目,我可能会考虑在从文件中提取数据时准备必要的值。我将使用 $new 数组作为输入(我在测试时修改了一些值)。

代码(Demo):

$new=[0=>['a0','a1','a2','a3'],1=>['a0','b1','b2','b3'],2=>['c0','b1','c2','c3'],3=>['d0','d1','c2','d3'],4=>['e0','e1','e2','f3']];

// OP says could be up to 10 columns...
for($x=0; $x<10; ++$x){
    $index_base=pow(10,2+$x)+1;         // or replace pow() with ** if php >=5.6
    // echo $x," : ",$index_base,"\n";  // uncomment if you want to see the values created
    $unique_col_val_keys[$x]=
        array_map(
            function(&$v)use($index_base){  // &$v means modify the input array's values
                return $v+$index_base;    // update the incremented ids (values) using $index_base
            },
            // the following 4 functions generate the input array to map
            array_flip(                   // swap keys and values within the array
                array_values(             // reset keys
                    array_unique(         // remove duplicate column values
                        array_column(
                            $new,$x       // get set of values from each column of multi-dim array
                        )
                    )
                )
            )
        );
}
//var_export($unique_col_val_keys);  // uncomment if you want to see the generated indexes

foreach($new as $row=>$a){
    if(!isset($result[$row])){$result[$row]=[];}
    foreach($a as $col=>$v){
        // echo "$row $col $v : {$unique_col_val_keys[$col][$v]}\n";
        array_push($result[$row],$v,"{$unique_col_val_keys[$col][$v]}");
    }
}
var_export($result);

输出:

array (
  0 => 
  array (
    0 => 'a0',
    1 => '101',
    2 => 'a1',
    3 => '1001',
    4 => 'a2',
    5 => '10001',
    6 => 'a3',
    7 => '100001',
  ),
  1 => 
  array (
    0 => 'a0',
    1 => '101',
    2 => 'b1',
    3 => '1002',
    4 => 'b2',
    5 => '10002',
    6 => 'b3',
    7 => '100002',
  ),
  2 => 
  array (
    0 => 'c0',
    1 => '102',
    2 => 'b1',
    3 => '1002',
    4 => 'c2',
    5 => '10003',
    6 => 'c3',
    7 => '100003',
  ),
  3 => 
  array (
    0 => 'd0',
    1 => '103',
    2 => 'd1',
    3 => '1003',
    4 => 'c2',
    5 => '10003',
    6 => 'd3',
    7 => '100004',
  ),
  4 => 
  array (
    0 => 'e0',
    1 => '104',
    2 => 'e1',
    3 => '1004',
    4 => 'e2',
    5 => '10004',
    6 => 'f3',
    7 => '100005',
  ),
)