Write a CSV file from a PHP Array

前端 未结 3 2074
走了就别回头了
走了就别回头了 2021-01-19 18:33

I appreciate there are already a lot of questions around this subject. But, as I\'m not a PHP developer, I\'m struggling to get anything to work for my specific object struc

3条回答
  •  清酒与你
    2021-01-19 18:38

    I've created a function for that before. Bare in mind the code bellow does not assume there is any csv headers.

    function doCSV($file, $content = array())
    {
        $handle =  fopen($file, "w");
        foreach($content as $value) {
            fputcsv($handle, array(key($content), $value));
        }
    
        fclose($handle);
    }
    
    doCSV("test.csv", array("Test 1" => "Value 1", "Test 2" => "Value 2"));
    

    If this is not what you're asking for let me know, I'll edit the answer.

提交回复
热议问题