title: Convert php array to csv string
body: <p>I have several method to transform php array to csv string both from stackoverflow and google. But I am in trouble that if I want to store mobile number such as 01727499452, it saves as without first 0 value.
  I am currently using this piece of code:
  <a href="https://stackoverflow.com/questions/3933668/convert-array-into-csv">Convert array into csv</a></p>
  
  <p>Can anyone please help me.</p>
  
  <pre><code>Array   
  
  [1] =&gt; Array
      (
          [0] =&gt; Lalu ' " ; \\ Kumar
          [1] =&gt; Mondal
          [2] =&gt; 01934298345
          [3] =&gt; 
      )
  
  [2] =&gt; Array
      (
          [0] =&gt; Pritom
          [1] =&gt; Kumar Mondal
          [2] =&gt; 01727499452
          [3] =&gt; Bit Mascot
      )
  
  [3] =&gt; Array
      (
          [0] =&gt; Pritom
          [1] =&gt; Kumar Mondal
          [2] =&gt; 01711511149
          [3] =&gt; 
      )
  
  [4] =&gt; Array
      (
          [0] =&gt; Raaz
          [1] =&gt; Mukherzee
          [2] =&gt; 01911224589
          [3] =&gt; Khulna University 06
      )
  </code></pre>
  
  <p>)</p>
  
  <p>My code block:</p>
  
  <pre><code>function arrayToCsv( array $fields, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false ) {
  $delimiter_esc = preg_quote($delimiter, '/');
  $enclosure_esc = preg_quote($enclosure, '/');
  
  $outputString = "";
  foreach($fields as $tempFields) {
      $output = array();
      foreach ( $tempFields as $field ) {
          if ($field === null &amp;&amp; $nullToMysqlNull) {
              $output[] = 'NULL';
              continue;
          }
  
          // Enclose fields containing $delimiter, $enclosure or whitespace
          if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) {
              $field = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
          }
          $output[] = $field." ";
      }
      $outputString .= implode( $delimiter, $output )."\r\n";
  }
  return $outputString; }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Pritom.</p>