title: PHP fgetcsv() search and overwrite file
body: <p>I have a .csv file with "code" and "description". I want to search the .csv and if it finds a duplicate "code", replace the whole row.</p>
  
  <p>ie. of csv file:</p>
  
  <pre><code>code          description
  STA-101       A great product
  STA-102       Terrible Product
  </code></pre>
  
  <p>If it were to find "STA-102" replace description with something, ie:</p>
  
  <pre><code>code          description
  STA-101       A great product
  STA-102       OK Product
  </code></pre>
  
  <p>These are dynamic so I need an if statement.  Can this be done using <code>fgetcsv()</code>?<br>
  The below doesn't work but its all I know right now:</p>
  
  <pre><code>$row = 1;
  if (($handle = fopen("formTest.csv", "r")) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $num = count($data);
          $row++;
          for ($c=0; $c &lt; $num; $c++) {
              if ($data[$c] = "STA-102") {
            $data[$c] = "OK Product"; }
              echo $data[$c] . "&lt;br /&gt;\n";
          }
  
      }
      fclose($handle);
  }
  </code></pre>
  
  <p>In order to write to my file to begin with, I have:</p>
  
  <pre><code>$code = $_POST['CODE'];
  $description = $_POST['DESC']; 
  
  if ($_POST) {
      $csvData = $code . "," . $description. "\n";
      $fp = fopen("formTest.csv","ab"); 
  
      if($fp){
      fwrite($fp,$csvData);
      fclose($fp); }
  }
  </code></pre>
  
  <p>How can I incorporate what I have above with the if statement to overwrite the code if it needs be?</p>