title: How to overwrite a csv file. PHP
body: <p>I only started PHP yesterday so forgive me in areas of poor code. </p>
  
  <p>I have a csv file called <code>employee_data.csv</code>. It contains employee records formatted like so.</p>
  
  <pre><code>JANE WILLIAMS,6/8/1998,55846874E,4323
  PETER JONES,15/01/1982,56897547Q,1234
  JAMES O'BRIEN,09/05/2001,25689514W,8475 
  </code></pre>
  
  <p>The employees are displayed on a form on the browser with each row containing an employee details and a delete button beside each row of the table. When the user selects the delete button beside a given employee, the selected employee gets deleted from the form and the employee_data.csv file and the new form is shown without the deleted employee.</p>
  
  <p><strong>delete table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Mh1jz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mh1jz.png" alt="enter image description here"></a></p>
  
  <p>I want to delete the selected record within the csv file but before I worry about the deleting part I just want to figure out how to overwrite the employee_details.csv with a new employee_details.csv with the same details.</p>
  
  <p>Too achieve this I want to</p>
  
  <p>1.implode the contents of the employee_data.csv file to a string.
  <br>2.explode the contents of the string to an array.
  <br>3.Delete employee.data.csv 
  <br>4.Create a new employee_data.csv file.
  <br>5.Write the contents of the array to the new employee_data.csv file
  <br>6.Output the employee records in the new file on to a table with each row containing an employee record.</p>
  
  <p>The $group array is an array of objects where each object is an employee record from the employee_data.csv file.
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php 
  $dataFile = fopen("persistence/employee_data.csv", "r") or die("Unable to open file!");
  $i=0;  //index for the array
  while(!feof($dataFile)) {
    $csv=fgets($dataFile); //read a line from the CSV file
    if(!feof($dataFile)){ //make sure not at end
     $employeeProperties=explode(",",$csv); //parse values to an array
            
            
          
          $group[$i]=new Employee($employeeProperties[0],$employeeProperties[1],$employeeProperties[2],$employeeProperties[3]);//create new person objects
     $i++;
    } 
  }
  fclose($dataFile); //close the data file
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I then imploded the contents of the $group array to a string, and then exploded the string to a string array.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  $str = implode(array_map(function($x) { return $x-&gt;get_employeeName() . $x -&gt;get_dob()  
          .$x-&gt;get_PPSN() .$x-&gt;get_PIN().","; }, $group));
  echo $str;   //implode file objects to string
  
  $parts = explode(',' , $str);  //explode to string array
  print_r($parts);
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The contents inside the $str - JANE WILLIAMS6/8/199855846874E4321 ,PETER JONES15/01/198256897547Q1234 ,JAMES O'BRIEN09/05/200125689514W8475 </p>
  
  <p>The contents of the array of string $parts Array ( [0] => JANE WILLIAMS6/8/199855846874E4321 <a href="https://i.stack.imgur.com/yBmyc.png" rel="nofollow noreferrer">2</a> => PETER JONES15/01/198256897547Q1234 <a href="https://i.stack.imgur.com/yBmyc.png" rel="nofollow noreferrer">2</a> => JAMES O'BRIEN09/05/200125689514W8475 [3] => ) </p>
  
  <p>I then deleted the orignal employee_data.csv, created a new employee_data.csv and wrote the string array to the csv file.
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  $file_Path = 'persistence/employee_data.csv';
  
  if(file_exists($file_Path))
      {
          unlink($file_Path);    //delete file
          echo 'File Deleted';
      }else{
          echo 'File Not Exist';
      }
      
      $csv_handler = fopen ('persistence/employee_data.csv','w'); //new file
      fwrite ($csv_handler,$csv);
      fclose ($csv_handler);
      
      $filename = 'persistence/employee_data.csv';
     if (file_exists($filename)) {
      $datafile = fopen('persistence/employee_data.csv', 'w'); //write to new file
      fputcsv($datafile, $parts," ",chr(0)); 
      fclose($datafile);
     }
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is what I want the contents of the new employee_data.csv file to look like. 
  <br>JANE WILLIAMS,6/8/1998,55846874E,4321
  <br>PETER JONES,15/01/1982,56897547Q,1234
  <br>JAMES O'BRIEN,09/05/2001,25689514W,8475</p>
  
  <p>Instead It looks like.
  <br>JANE WILLIAMS6/8/199855846874E4321
  <br> , PETER JONES15/01/198256897547Q1234
  <br> , JAMES O'BRIEN09/05/200125689514W8475
  <br> ,</p>
  
  <p>In the new csv file there are now 4 records, the last one doesnt contain anything and I'm not sure why this gets added. I want to just have a comma after the name,dob and ppsn. The last 4 digits of each record is a pin.</p>
  
  <p>Is there a more effiecient way to overwrite an csv file? Is there any way to achieve what I want to achieve? I've been messing around with the format of the new file for the past hour and thats the best I have come up with so far.</p>
  
  <h1>Note: I have to update any changes made to the csv file in my project so if I delete an employee record from the csv file, I need a new or overwritten csv file without the deleted employee.</h1>