Create CSV from multidimensional array with fputcsv

后端 未结 2 1576
小蘑菇
小蘑菇 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, ...]
    

提交回复
热议问题