简体   繁体   中英

Export data to CSV file using PHP - output is empty

I thought it will be easy task, but simple I'm not able to output data from an array to CSV file (saved on a server).

Code for that part looks like:

$fp = fopen('missing-skus.csv', 'w');

foreach ($missing_array as $lines) {
    fputcsv($fp, $lines);
}

fclose($fp);

$missing_array looks like:

Array
(
    [0] => 5804
    [1] => 5803
    [2] => 5802
    [3] => 5801
    [4] => 5800
    [5] => 5799
    [6] => 5798
    [7] => 5797
    [8] => 5796
    [9] => 5795
    [10] => 5794
    [11] => 5793
    [12] => 5792
    [13] => 5791
    [14] => 5790
    [15] => 5789
    [16] => 5788
    [17] => 5787
    [18] => 5786
    [19] => 5785
    [20] => 5784
    [21] => 5783
    [22] => 5782
    [23] => 5781
    [24] => 5780
    [25] => 5779
)

No matter what, file is always blank. Any clue what I have missed?

The second argument to fputcsv is an array of fields. You are passing a string or integer.

If you want one line with the array values as fields, in CSV fashion, then just:

$fp = fopen('missing-skus.csv', 'w');
fputcsv($fp, $missing_array);

If you just want each array value on one line then no need for fputcsv :

file_put_contents('missing-skus.csv', implode("\n", $missing_array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM