简体   繁体   中英

Export php array to csv rows instead of columns

Is there a way to put a php array in a csv file entering every item of the array as a new row? This is what I have:

$list = array (
        array('names'),
        array('bob,bill,john,sally'));

    $fp = fopen('files.csv', 'w');

    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }

    fclose($fp);

My csv looks like this:

names
bob,bill,john,sally

However this is what I'm looking for:

names
bob
bill
john
sally

It was just a matter of using fwrite() instead of fputcsv()

$list = array (
        array('names'),
        array('bob,bill,john,sally'));

    $fp = fopen('files.csv', 'w');

    foreach ($list as $fields) {
        fwrite($fp, $fields.PHP_EOL);
    }

    fclose($fp);

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