我需要帮助从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列是第4列的母亲(但它是一个explosion (',', Column 3))在第4栏爆炸的是5个孩子的母亲
我想生成一个这种类型的数组(并创建了一个ID从50开始),因为目标是将这个文件转换为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)
我尝试了一堆方法,我开始绝望。每件事我都没有达到预期的结果,所以我从周一开始就一直在努力,我想上吊自杀。
试试这样:
<?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++;
}
}
}
}
?>
再次感谢你,艾曼。这是我的代码,里面包含了你给我的音轨。
$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;
}
}