Convert php array to csv string

后端 未结 8 1375
一向
一向 2020-12-10 02:47

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,

8条回答
  •  再見小時候
    2020-12-10 03:12

    You could use str_putcsv function:

    if(!function_exists('str_putcsv'))
    {
        function str_putcsv($input, $delimiter = ',', $enclosure = '"')
        {
            // Open a memory "file" for read/write...
            $fp = fopen('php://temp', 'r+');
            // ... write the $input array to the "file" using fputcsv()...
            fputcsv($fp, $input, $delimiter, $enclosure);
            // ... rewind the "file" so we can read what we just wrote...
            rewind($fp);
            // ... read the entire line into a variable...
            $data = fread($fp, 1048576);
            // ... close the "file"...
            fclose($fp);
            // ... and return the $data to the caller, with the trailing newline from fgets() removed.
            return rtrim($data, "\n");
        }
     }
    
     $csvString = '';
     foreach ($list as $fields) {
         $csvString .= str_putcsv($fields);
     }
    

    More about this on GitHub, a function created by @johanmeiring.

提交回复
热议问题