2011-11-17 87 views
2

我有两个文件,一个是划定一个标签,一个是CSV是逗号分隔,他们都得到上传,我需要将它们组合成一个排序的CSV这就是......这里是我的代码到目前为止如何将两个CSV /制表符分隔到一个CSV与PHP?

$txt = glob('files/*.txt*'); 
$csv = glob('files/*.csv*'); 
$test = array(); 
if (($handle = fopen($csv[0], "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $num = count($data); 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      $test[$c][] = $data[$c]; 
     } 
    } 
    fclose($handle); 
} 

if (($handle = fopen($txt[0], "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { 
     $num = count($data); 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      $test[$c][] = $data[$c]; 
     } 
    } 
    fclose($handle); 
} 

测试阵列既具有全阵列,但我遇到了一些问题,首先是$测试阵列是这样

[0] => Array 
     (
      [0] => Edit 
      [1] => y 
      [2] => y 
      [3] => y 
      [4] => y 
      [5] => y 
      [6] => y 
      [7] => y 
      [8] => y 
      [9] => y 
[1] => Array 
    (
     [0] => Event 
     [1] => Carolina Panthers PSL 
     [2] => Florida 
     [3] => Carolina Panthers PSL 
     [4] => Apple 
     [5] => Carolina Panthers PSL 
     [6] => Carolina Panthers PSL 
     [7] => Carolina Panthers PSL 
     [8] => Carolina Panthers PSL 
     [9] => Carolina Panthers PSL 
     [10] => Carolina Panthers PSL 
[2] => Array 
    (
     [0] => Venue 
     [1] => Bank of America Stadium 
     [2] => Washington Mutual 
     [3] => Bank of America Stadium 
     [4] => Apple Inc 
     [5] => Bank of America Stadium 
     [6] => Bank of America Stadium 
     [7] => Bank of America Stadium 
     [8] => Bank of America Stadium 
     [9] => Bank of America Stadium 
     [10] => Bank of America Stadium 
     [11] => Bank of America Stadium 
     [12] => Bank of America Stadium 

如何创建的一切新的CSV,但我需要通过事件排序名称和测试阵列的这种格式似乎关闭..任何想法我做错了什么...

回答

3

假设列名的文件的第一行这应该工作:

$txt = glob('files/*.txt*'); 
$csv = glob('files/*.csv*'); 
$test = array(); 
$headers = array(); 
if (($handle = fopen($csv[0], "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    $num = count($data); 
    if (empty($headers)) { 
     for ($c=0; $c < $num; $c++) { 
     $headers[$c] = $data[$c]; 
     } 
    } 
    else { 
     $line = array(); 
     for ($c = 0; $c < $num; $c++) { 
     $line[$headers[$c]] = $data[$c]; 
     } 
     $test[] = $line; 
    } 
    } 
} 

if (($handle = fopen($csv[0], "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    $num = count($data); 
    $line = array(); 
    for ($c = 0; $c < $num; $c++) { 
     $line[$headers[$c]] = $data[$c]; 
    } 
    $test[] = $line; 
    } 
} 

这应该给你喜欢

[0] => Array 
(
     [Edit] => y 
     [Event] => Carolina Panthers PSL 
     [Venue] => Bank of America Stadium, 
     ... 
[1] => Array 
(

     [Edit] => y 
     [Event] => Florida 
     [Venue] => Washington Mutual , 
... 

一个数组,那么你可以使用usort该数组按照自定义回调由Event键排序。请参阅该功能的文档页面的例子负荷,或试试这个:

function my_sort($a, $b) { 
    return strcmp($a['Event'], $b['Event']); 
} 
usort($test, 'my_sort'); 
+0

我喜欢这个答案....但有没有一种方法来排序数组,但事件名称 – Trace

+0

@Tamer:只是更新答案希望可以帮助 – Clive

2

下面的代码应该适合你,我相信。

<?php 
// Opens files 
$file1 = glob("files/*.txt"); 
$file1 = $file1[0]; 
$f1 = file("files/$file1"); 
$file2 = glob("files/*.csv"); 
$file2 = $file2[0]; 
$f2 = file("files/$file2"); 

// Creates counter and storage arrays for each column 
$c = 0; 
$col1 = array(); 
$col2 = array(); 
$col3 = array(); 

// goes through each of the first files lines 
foreach ($f1 as $row => $data) 
{ 
    // changes tabs for commas 
    $data .= str_replace("\t",",",$data); 
    // splits the string into the 3 columns in different arrays 
    list($col1[$c],$col2[$c],$col3[$c]) = explode(",",$data); 
    $c++; 
} 

foreach ($f2 as $row2 => $data2) 
{ 
    if ($row2 != 0) 
    { 
     // splits the string into the 3 columns in different arrays 
     list($col1[$c],$col2[$c],$col3[$c]) = explode(",",$data2); 
     $c++; 
    } 
} 
// Sort by column 2, the event 
asort($col2); 

// First column is headers 
$realOutput = $col1[0].",".$col2[0].",".$col3[0]."\n"; 

// Add them in sorted order by column 2 
foreach ($col2 as $key => $val) 
{ 
    if ($key != 0) 
     $realOutput .= $col1[$key].",".$col2[$key].",".$col3[$key]."\n"; 
} 

echo $realOutput; 

?> 
+0

我没有这个文件的名称虽然...我只有扩展 – Trace

+0

还我如何得到最终的CSV打印头还...任何想法...两个文件具有完全相同的标题 – Trace

+0

我添加了glob命令来查找一个TXT和一个CSV文件。我还添加了一些if语句,只将这些头文件放入一次,然后仅将它们显示在文件的顶部。 – ckimbrell

2

EDITED占在同一顺序不是列,避免列标题存在于排序的数据。这仍然依赖列标题是相同的(包括相同的情况)。也是固定SORTDESC(应为SORT_DESC

$txt = glob('files/*.txt*'); 
$csv = glob('files/*.csv*'); 
$rows = $events = array(); 

// Get data into rows, not columns 
if (($handle = fopen($csv[0], "r")) !== FALSE) { 
    // Get column headers 
    $headers = fgetcsv($handle, 1000, ","); 
    // Get rest of rows 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $row = array(); 
     foreach ($data as $k => $v) $row[$headers[$k]] = $v; 
     $rows[] = $row; 
    } 
    fclose($handle); 
} 

if (($handle = fopen($txt[0], "r")) !== FALSE) { 
    // Get column headers 
    $headers = fgetcsv($handle, 1000, "\t"); 
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) { 
     $row = array(); 
     foreach ($data as $k => $v) $row[$headers[$k]] = $v; 
     $rows[] = $row; 
    } 
    fclose($handle); 
} 

// Prepare the sort column for array_multisort() 
foreach ($rows as $key => $row) { 
    $events[$key] = $row['Event']; 
} 

// Sort it 
array_multisort($events, SORT_DESC, $rows); 

// Open the output file 
$outFile = fopen('outfile.csv','w'); 

// Put the column headers back 
fputcsv($outFile, array('Edit','Event','Venue'), ','); 

// Write the data 
foreach ($rows as $row) { 
    fputcsv($outFile, $row, ','); 
} 

// Close the file 
fclose($outFile);