将php数组转换为csv字符串 [英] Convert php array to csv string

查看:199
本文介绍了将php数组转换为csv字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个方法来转换php数组到csv字符串,从stackoverflow和谷歌。但我有麻烦,如果我想存储手机号码,如01727499452,它保存为没有前0值。
我目前正在使用这段代码:
将数组转换为csv



任何人都可以帮助我。

  b 
[1] =>数组

[0] => Lalu'; \\ Kumar
[1] => Mondal
[2] => 01934298345
[3] =>
[2] =>
[2] =& => Kumar Mondal
[2] => 01727499452
[3] => Bit Mascot


[3] =& $ b(
[0] => Pritom
[1] => Kumar Mondal
[2] => 01711511149
[3] =>


[4] => Array

[0] => Raaz
[1] => Mukherzee
[2 ] => 01911224589
[3] => Khulna大学06



我的代码块:

  function arrayToCsv $ 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&& $ nullToMysqlNull){
$ output [] ='NULL';
continue;
}

//包含$ delimiter,$ enclosure或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\\\
;
}
return $ outputString; }

感谢,



Pritom。 / p>

解决方案

您可以使用 str_putcsv 函数:

  if(!function_exists('str_putcsv'))
{
function str_putcsv($ input,$ delimiter =',',$
{
//为读/写打开一个内存文件...
$ fp = fopen('php:// temp','r + );
// ...使用fputcsv()将$ input数组写入file...
fputcsv($ fp,$ input,$ delimiter,$ enclosure);
// ...回滚文件,以便我们可以读取我们刚刚写的内容...
rewind($ fp);
// ...将整行读入变量。 。
$ data = fread($ fp,1048576);
// ...关闭文件...
fclose($ fp);
//。 。
return rtrim($ data,\\\
);
}
}

$ csvString ='';
foreach($ list as $ fields){
$ csvString。= str_putcsv($ fp,$ fields);
}

有关 GitHub ,由@johanmeiring创建的函数。


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: Convert array into csv

Can anyone please help me.

Array   

[1] => Array
    (
        [0] => Lalu ' " ; \\ Kumar
        [1] => Mondal
        [2] => 01934298345
        [3] => 
    )

[2] => Array
    (
        [0] => Pritom
        [1] => Kumar Mondal
        [2] => 01727499452
        [3] => Bit Mascot
    )

[3] => Array
    (
        [0] => Pritom
        [1] => Kumar Mondal
        [2] => 01711511149
        [3] => 
    )

[4] => Array
    (
        [0] => Raaz
        [1] => Mukherzee
        [2] => 01911224589
        [3] => Khulna University 06
    )

)

My code block:

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 && $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; }

Thanks,

Pritom.

解决方案

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($fp, $fields);
 }

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

这篇关于将php数组转换为csv字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆