将php数组数据导出到CSV或Excel

时间:2018-11-28 08:04:55

标签: php arrays excel csv

变量$ list存储一个类似于下面的数组。 我需要将数组传递给CSV或XLS,并发送标题以下载文件。

print_r($ list)显示以下内容:

Array
  (
    [0] => Array
       (
         [warehouse] => Array
            (
              [warehouseName] => Warehouse Europe
              [warehouseType] => en detail
            )

          [products] => Array
            (
                [0] => Array
                    (
                        [productName] => APPAREL SHIRTS
                        [productCode] => 54059761696
                        [measuringUnit] => buc
                        [quantity] => 1
                    )

                [1] => Array
                    (
                        [productName] => T-SHIRTS - SADAL
                        [productCode] => 54059764755
                        [measuringUnit] => buc
                        [quantity] => 0
                    )


      ........... more data ............


             [5315] => Array
                    (
                        [productName] => Ski - COLBECK 
                        [productCode] => 54080519051
                        [measuringUnit] => buc
                        [quantity] => 3
                    )



              )

    )

[1] => Array
    (
        [warehouse] => Array
            (
                [warehouseName] => Warehouse US
                [warehouseType] => en detail
            )

        [products] => Array
            (
                [0] => Array
                    (
                        [productName] => T-SHIRTS - SHEW
                        [productCode] => 40059763901
                        [measuringUnit] => buc
                        [quantity] => 5
                    )

                [1] => Array
                    (
                        [productName] => SHOESSHOES - OPTIMA
                        [productCode] => 45063556796
                        [measuringUnit] => buc
                        [quantity] => 2
                    )

 ........... more data ............

                [3178] => Array
                    (
                        [productName] => Ski - CASTINE
                        [productCode] => 40080490213
                        [measuringUnit] => buc
                        [quantity] => 1
                    )

            )

      )

)

在excel中,CSV看起来像这样:example 我尝试了几次,但我无法应付。我认为一个foreach函数和fputcsv应该可以解决问题。

/////之后编辑/////

这解决了,谢谢!

 foreach($list as $item){
     foreach($item['products'] as $product){
         $wName = $item['warehouse']['warehouseName'];
         $wType = $item['warehouse']['warehouseType'];
         $pr =implode(',',$product);
         printf("%s,%s,%s".PHP_EOL,$wName,$wType,$pr);
     }
 }

1 个答案:

答案 0 :(得分:0)

我使用了更多变量以便于理解。您可以减少它们,并可以将 printf echo 函数替换为 sprintf file_put_content 以及所需的所有内容

foreach($list as $item)
{
    foreach($item['products'] as $product){
         $wName = $item['warehouse']['warehouseName'];
         $wType = $item['warehouse']['warehouseType'];
         $pr =implode(',',$product);
         printf("%s,%s,%s".PHP_EOL,$wName,$wType,$pr);
   }
   echo "...,...,...,...,...,...";
}