title: how to order a big csv file with php?
body: <p>i'm looking for an algorithm strategy. I have a csv file with 162 columns and 55000 lines.
  I want to order the datas with one date (which is on column 3).</p>
  
  <p>first i tried directly to put everything in an array, but memory explodes.</p>
  
  <p>So i decided to :
  1/ Put in an array the 3 first columns.
  2/ Order this array with usort
  3/ read the csv file to recover the other columns
  4/ Add in a new csv file the complete line
  5/ replace the line by an empty string on the readed csv file</p>
  
  <pre><code>//First read of the file
  while(($data = fgetcsv($handle, 0,';')) !== false)
  {
      $tabLigne[$columnNames[0]] = $data[0];
      $tabLigne[$columnNames[1]] = $data[1];
      $tabLigne[$columnNames[2]] = $data[2];
  
      $dateCreation = DateTime::createFromFormat('d/m/Y', $tabLigne['Date de Création']);
  
  
  
      if($dateCreation !== false)
      {
          $tableauDossiers[$row] = $tabLigne;
      }
      $row++; 
  
      unset($data);
      unset($tabLigne);
  }
  
  //Order the array by date
  
  usort(
      $tableauDossiers,
      function($x, $y) {
          $date1 = DateTime::createFromFormat('d/m/Y', $x['Date de Création']);
          $date2 = DateTime::createFromFormat('d/m/Y', $y['Date de Création']);
  
          return $date1-&gt;format('U')&gt; $date2-&gt;format('U');
  
      }
  );
  
  
  fclose($handle);
  copy(PATH_CSV.'original_file.csv', PATH_CSV.'copy_of_file.csv');
  
  
  
  for ($row = 3; $row &lt;= count($tableauDossiers); $row++)
  
  {
      $handle = fopen(PATH_CSV.'copy_of_file.csv', 'c+');
      $tabHandle = file(PATH_CSV.'copy_of_file.csv');
      fgetcsv($handle);
      fgetcsv($handle);
      $rowHandle = 2;
      while(($data = fgetcsv($handle, 0,';')) !== false)
      {
  
          if($tableauDossiers[$row]['Caisse Locale Déléguée'] == $data[0]
                  &amp;&amp; $tableauDossiers[$row]['Date de Création'] == $data[1]
                  &amp;&amp; $tableauDossiers[$row]['Numéro RCT'] == $data[2])
          {
              fputcsv($fichierSortieDossier, $data,';');
              $tabHandle[$rowHandle]=str_replace("\n",'', $tabHandle[$rowHandle]);
              file_put_contents(PATH_CSV.'copy_of_file.csv', $tabHandle);
              unset($tabHandle);
  
              break;
          }
          $rowHandle++;
          unset($data);
          unset($tabLigne);
      }
  
      fclose($handle);
      unset($handle);
  }
  </code></pre>
  
  <p>This algo is really too long to execute, but works</p>
  
  <p>Any idea how to improve it ?</p>
  
  <p>Thanks</p>