Search code examples
phpcsvfgetcsv

How to combine two csvs/tab delimited into one csv with php?


I have two files one is a tab delimited and one is a csv which is comma seperated and they both get uploaded and I need to combine them into one csv thats sorted...here is my code so far

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

the test array has the full array of both but i am running into a few problems first the $test array is like this

[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

How do i create a new csv with everything but i need to sort by event name and this format for the test array seems off..any ideas what im doing wrong...


Solution

  • Assuming the column names are on the first line of your file this should work:

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

    That should give you an array like

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

    Then you can use usort on the array to sort it by the Event key with a custom callback. See the docs page for that function for loads of examples, or try this:

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