Create CSV from multidimensional array with fputcsv

后端 未结 2 1550
小蘑菇
小蘑菇 2021-01-19 05:48

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
            


        
相关标签:
2条回答
  • 2021-01-19 05:54

    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, ...]
    
    0 讨论(0)
  • 2021-01-19 06:02
    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

    0 讨论(0)
提交回复
热议问题