Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php将两个csv/tab分隔符组合成一个csv?_Php_Csv_Fgetcsv - Fatal编程技术网

如何使用php将两个csv/tab分隔符组合成一个csv?

如何使用php将两个csv/tab分隔符组合成一个csv?,php,csv,fgetcsv,Php,Csv,Fgetcsv,我有两个文件,一个是制表符分隔的,一个是逗号分隔的csv,它们都被上传,我需要将它们合并成一个排序的csv…这是我到目前为止的代码 $txt = glob('files/*.txt*'); $csv = glob('files/*.csv*'); $test = array(); if (($handle = fopen($csv[0], "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

我有两个文件,一个是制表符分隔的,一个是逗号分隔的csv,它们都被上传,我需要将它们合并成一个排序的csv…这是我到目前为止的代码

$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);
}

如何创建一个包含所有内容的新csv,但我需要按事件名称排序,并且测试数组的这种格式似乎不正确。任何关于我做错了什么的想法…

我相信下面的代码应该适合您

<?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;

?>

假设列名位于文件的第一行,则应该可以:

$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;
  }
}
然后,您可以在数组上使用自定义回调按
事件
键对其进行排序。有关该函数的示例加载,请参见文档页面,或者尝试以下操作:

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

编辑以说明列的顺序不同,并避免排序数据中出现列标题。这仍然依赖于列标题相同(包括相同的大小写)。还修复了
SORTDESC
(应为
SORT\u DESC


我没有文件的名称,但是…我只有扩展名,所以我如何得到最终的csv来打印标题…任何想法…两个文件都有完全相同的标题我添加了glob命令来查找一个TXT和一个csv文件。我还添加了几个if语句,只将标题放入一次,然后仅在文件顶部显示它们。
[0] => Array
(
       [Edit] => y
       [Event] => Carolina Panthers PSL
       [Venue] => Bank of America Stadium,
       ...
[1] => Array
(

       [Edit] => y
       [Event] => Florida
       [Venue] => Washington Mutual ,
...
function my_sort($a, $b) {
  return strcmp($a['Event'], $b['Event']);
}
usort($test, 'my_sort');
$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);