使用csv文件生成类别树

时间:2011-09-21 10:15:23

标签: php arrays csv multidimensional-array tree

我需要帮助才能从CSV文件中生成类别树。

我做了很多事,但我迷路了......

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

"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (500 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (1000 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAIN FRAME", "OPERATOR CAB"

在这些方面, 第1列表示父类 第2列是第1列的子代 第3列是第2列的子代 第3列是第4列的母亲(但它是爆炸性的(',',第3列)) 第4栏中的爆炸是5的母亲

我想生成这种类型的数组(并创建一个从50开始的ID),因为目标是将此文件转换为mysql DB:s

我会成功地拥有这样的东西

LOADERS (id = 50)
|___S130 (id = 51)
|----|____Above & 524611001 (id = 52)
|-----------|____ MAINTENANCE ITEMS (id = 53)
|------------------|____SCHEDULED MAINTENANCE ITEMS (500 HOUR) (id = 54)
|------------------|____SCHEDULED MAINTENANCE ITEMS (1000 HOUR) (id = 55)
|-----------|____ MAIN FRAME (id = 56)
|------------------|____OPERATOR CAB (id = 57)
|----|____Above & 524711001 (id = 58)
|-----------|____ MAINTENANCE ITEMS (id = 59)
|------------------|____SCHEDULED MAINTENANCE ITEMS (500 HOUR) (id = 60)
|------------------|____SCHEDULED MAINTENANCE ITEMS (1000 HOUR) (id = 61)
|-----------|____ MAIN FRAME (id = 62)
|------------------|____OPERATOR CAB (id = 63)

我尝试了一堆方法,我开始绝望了。我甚至没有达到预期的结果:我从星期一开始就出汗了,我想自己上吊。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<?php
// generate categories tree with csv file
// http://stackoverflow.com/questions/7498119/generate-categories-tree-with-csv-file

$big_array  = array();
$temp_array = array();

$csv = <<< CSV
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (500 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAINTENANCE ITEMS", "SCHEDULED MAINTENANCE ITEMS (1000 HOUR)"
"LOADERS", "S130", "Above & 524611001, 524711001 & Above", "MAIN FRAME", "OPERATOR CAB"
CSV;

foreach(preg_split("/(\r?\n)/", $csv) as $line) {

    list($column1, $column2, $column3, $column4) = explode('", "', $line);

    $column1 = preg_replace('/^"/', '', $column1);
    $column4  = preg_replace('/\"(?<!\\")/', '', $column4);

    foreach(explode(', ', $column3) as $column3_part) {
        $temp_array[$column3_part][] = $column4;
    }


    $big_array[$column1][$column2] = $temp_array;
}

echo '<pre>' . print_r($big_array, 1) . '</pre><hr />';

$counter = 50;
foreach($big_array as $key1 => $level1) {
    foreach($level1 as $key2 => $level2) {
        foreach($level2 as $key3 => $level4) {
            foreach($level4 as $level5) {

                echo '<pre>' . print_r('INSERT INTO DATABASE: ' . $counter . ', ' . $key1 . ', ' . $key2 . ', ' . $key3 . ', ' . $level5, 1) . '</pre>';
                $counter++;
            }
        }
    }
}
?>

答案 1 :(得分:0)

非常感谢艾曼。这是我的代码,其中包含您给我的曲目。

$big_array  = array();
$temp_array = array();

if (($handle = fopen("arbocsv.csv", "r")) !== FALSE) 
{
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
    {

        $serials = preg_replace('/ /','',$data[2]);
    $serials = preg_replace('/&/',' & ',$serials);
    $serials = explode(',',$serials);

    foreach($serials as $serial)
    {
        $temp_array[trim($serial)][trim($data[3])][] = trim($data[4]); 
    }

    $big_array[trim($data[0])][trim($data[1])] = $temp_array;


}
}