php make csv文件的数组 [英] php make csv file for the array

查看:102
本文介绍了php make csv文件的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组。当我使用 print_r($ output)。我得到了这样的数组

I have an array. When I am using print_r($output). I am getting the array like this

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文件。所以我这样做我的代码

I want to export this to a csv file. So for that I made my code like this

$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();

但是这里我得到文件,但是当我打开文件时, 。所以有人可以告诉我如何获取csv中的数据?任何帮助和建议将真正可感知。感谢

But here I am getting the file but when I am opening the file it is showing only array(). So can someone tell me how to get the data in csv? Any help and suggestions will be really appreciable. Thanks

推荐答案

您的数组不是真正的CSV友好,因为它是多维的。

Your array isn't really CSV friendly as it is multi-dimensional.

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

To put into CSV, you need to convert each element of $output to a one-dimensional array:

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);
}

这篇关于php make csv文件的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆