php为数组制作csv文件

时间:2015-06-02 08:00:45

标签: php csv fputcsv

我有一个数组。当我使用print_r($output)时。我正在获得像这样的数组

array(
    [0] => Array
            (
                [listing_id] => 14
                [category_id] => Modern Australian
                [listing_name] => Boatshed Restaurant
                [image_name] => krish_logo.png
                [address] => 2, Thrower Drive
                [phone] => 07 5534 3888
                [introduction_text] => 
                [website] => 
                [payment_types] => 
                [open_days] => 
                [licenced] => 0
                [highchair] => 0
                [dress_code] => 

            )
    [1] => Array
            (
                [listing_id] => 13
                [category_id] => Indian,Restaurant,Take-away
                [listing_name] => Krish Indian Cuisine - Varsity Lakes
                [image_name] => krish_logo.png
                [address] => Shop B/228 Varsity Parade, The Piazza Varsity Lakes
                [phone] => 07 5578 8090
                [introduction_text] => <p>Welcome to Krish Indian Cuisine. An award winning north indian  restaurant which offers the very best in modern and indian-fusion  cuisine. If you prefer your food tantalizingly hot or subtly mild, Krish  Indian has something for you. Bring the whole family or maybe just an  intimate dinner for two. Whatever the reason you will enjoy the fabulous  food</p>
                [website] => http://www.testsite.com/
                [payment_types] => Cash,Visa,Mastercard,Eftpos
                [open_days] => TuesLunch,n,MonLunch,n,MonDinner,n,TuesBKfast,n,WedLunch,n,ThuDinner,n,ThuLunch,n,TuesDinner,n,WedDinner,n,FriDinner,n,FriLunch,n,SunDinner,n,SatLunch,n,SatDinner,n,SunLunch,n
                [licenced] => 0
                [highchair] => 1
                [dress_code] => Casual



            )
    [2] => Array
            (
                [listing_id] => 12
                [category_id] => Steak
                [listing_name] => Outback Jacks Bar & Grill - Southport
                [image_name] => 9_1552272162010moomoo.jpg
                [address] => 2, Barney Street
                [phone] => 07 5532 3271
                [introduction_text] => 
                [website] => 
                [payment_types] => 
                [open_days] => 
                [licenced] => 0
                [highchair] => 0
                [dress_code] => 

            )                

)

我想将其导出到csv文件。所以我为此制作了我的代码

$fichier = 'file.csv';
 header( "Content-Type: text/csv;charset=utf-8" );
 header( "Content-Disposition: attachment;filename=\"$fichier\"" );
 header("Pragma: no-cache");
 header("Expires: 0");

 $fp= fopen('php://output', 'w');

 foreach ($output as $fields) 
 {
    fputcsv($fp, $fields);
 }
 fclose($fp);
 exit();

但是我在这里获取文件但是当我打开文件时它只显示了array()。那么有人能告诉我如何在csv中获取数据吗?任何帮助和建议都会非常明显。感谢

2 个答案:

答案 0 :(得分:2)

您的数组不具有CSV友好性,因为它是多维的。

要放入CSV,您需要将$ output的每个元素转换为一维数组:

foreach ($output as $fields) {

   $csvrec = array(
        'listing_id' => $fields['listing_id'],
        'category_id' => $fields['category_id'], 
   // etc... etc...
   // down to...
        'parking' => $fields['dinning_details']['parking'],
        'byo_info' => $fields['dinning_details']['byo_info'],
        'indoor' => $fields['dinning_details']['capacity']['indoor'],
   // etc.. etc...
   );

   fputcsv($fp, $csvrec);
}

答案 1 :(得分:0)

将嵌套数组传递给fputcsv时,内部数组将显示为&#39;数组&#39;。您应首先展平阵列或以与所需CSV格式相对应的方式对其进行格式化。