Create CSV from multidimensional array with fputcsv

限于喜欢 提交于 2019-12-19 19:46:44

问题


I'm trying to get a multidimensional array into a csv file. data in the array is as such:

Array
(
 [0] => Array
    (
        [product_id] => 1111
        [name] => Alcatel One Touch Idol 2
        [keyword] => alcatel-one-touch-idol-2
        [options] => Array
            (
                [0] => Array
                    (
                        [price] => 54.0000
                    )

                [1] => Array
                    (
                        [price] => 42.0000
                    )

                [2] => Array
                    (
                        [price] => 10.0000
                    )

                [3] => Array
                    (
                        [price] => 
                    )

                [4] => Array
                    (
                        [price] => 
                    )

                [5] => Array
                    (
                        [price] => 
                    )

                [6] => Array
                    (
                        [price] => 
                    )

                [7] => Array
                    (
                        [price] => 
                    )

                [8] => Array
                    (
                        [price] => 
                    )

                [9] => Array
                    (
                        [price] => 
                    )

            )

    )

[1] => Array
    (...... etc)

I get all of the top level data, but then once it reaches the options array, i get Array to string conversion errors. So i have two issue, i firstly need to see why i am getting this error, and then i need to fputcsv all of this information on one line per product.

So far i have this to parse the array into a csv:

$output = fopen("php://output",'w') or die("Can't open php://output");
            header("Content-Type:application/csv"); 
            header("Content-  Disposition:attachment;filename=product_catalog.csv"); 
            $first_line = explode(",", $first_line);
            fputcsv($output, $first_line);
            foreach($csv as $file) {
                fputcsv($output, $file);
            }
            fclose($output) or die("Can't close php://output");

If anyone could help me i'd really appreciate that. regards.


回答1:


I would suggest to flatten each array first:

foreach ($csv as $file) {
    $result = [];
    array_walk_recursive($file, function($item) use (&$result) {
        $result[] = $item;
    });
    fputcsv($output, $result);
}

In each iteration it would create an array like this:

[1111, 'Alcatel One Touch Idol 2', 'alcatel-one-touch-idol-2', 54, 42, ...]



回答2:


foreach ($data as $line) {
    foreach($line as $array){
        foreach($array as $result){
            fputcsv($f,$result,",");
        }
    }
}

i know its an old question but you could also use this



来源:https://stackoverflow.com/questions/28322751/create-csv-from-multidimensional-array-with-fputcsv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!