fputcsv() 函数用于将数据格式为csv格式,以便写入文件或者数据库。

下面代码演示了如何使用fputcsv()函数将字符串写入csv文件中:

$test_array = array(
        array(\"php\",\"java\",\"css\",\"mysql\",\" \"),
        array(\"html\",\" \",\"js\",\"apache\",\"json\"),
        array(\"csv\",\"file\",\"sql\",\"css3\",\"bootstrap\"),
        array(\"jquery\",\"editor\",\"thinkphp\",\"ci\",\"html\")
    );
    /* http://www.manongjc.com/article/1311.html */
    $file = fopen(\"test.csv\",\"w\") or die(\"Can\'t Open test.csv\");
    foreach($test_array as $line_array)
    {
        $isSuccess = fputcsv($file,$line_array);
        print $isSuccess.\"<br>\";     if($isSuccess===false)
        {
            die(\"Can\'t write csv line\".$line_array);
        }
    }
    fclose($file) or die(\"Can\'t close file test.csv.\");
收藏 打印