将数组转换为csv时出现php- extra逗号

I have an array which looks somewhat like this

$cars = array
  (
  array("Name","Price","Hours"),
  array("AMW",15,13),
  array("Saab",5,2),
  array("Cand Rover",17,15)
  );

And I'm trying to convert this array to CSV data so that I can write it into a file.

I have a function which takes an array as input and converts it into CSV format. It looks like this:

function array_2_csv($array) 
{
     $csv = array();
     foreach ($array as $item) 
     {
         if (is_array($item)) 
         {
             $csv[] = array_2_csv($item);
             $csv[]="
";
         } 
         else 
         {
             $csv[] = $item;
         }
     }
     return implode(',', $csv);
}

It works but an extra comma appears on front of lines. How can I remove that?

Expected Output

Name,Price,Hours
AMW,15,13,
Saab,5,2,
Cand Rover,17,15

Output I'm getting is:

Name,Price,Hours,
,AMW,15,13,
,Saab,5,2,
,Cand Rover,17,15,

Full Code

$cars = array
  (
  array("Name","Price","Hours"),
  array("AMW",15,13),
  array("Saab",5,2),
  array("Cand Rover",17,15)
  );
$csv_data = array_2_csv($cars);

echo $csv_data;
function array_2_csv($array) 
{
     $csv = array();
     foreach ($array as $item) 
     {
         if (is_array($item)) 
         {
             $csv[] = array_2_csv($item);
             $csv[]="
";
         } 
         else 
         {
             $csv[] = $item;
         }
     }
     return implode(',', $csv);
}

Do like below (your code modification):-

$csv_data = array_2_csv($cars);

echo $csv_data;
function array_2_csv($array) 
{
     $csv = ''; //take string
     foreach ($array as $item) 
     {
         if (is_array($item)) 
         {
             $csv .= implode(',',$item); // implode and add to string
             $csv .="
"; // add new line
         }else{
           $csv .= $item; // direct add value to string
           $csv .="
"; //add new line
         } 

     }
     return $csv; //return string
}

Output:-https://eval.in/752820

If you're going to be writing the contents to a file anyway then why not cut out the middleman and just use fputcsv().

This way you function would look something like:

function array_2_csv($array, $fileName)
{
    $file = fopen($fileName, 'w');

    foreach ($array as $fields) {
        fputcsv($file, $fields);
    }

    fclose($file);
}

Then your call would be something like:

array_2_csv($cars, 'path-to-file')

Hope this helps!

You can do more functional style and use array_map along with couple of implode functions:

function array_2_csv(array $array, $delimiter = ',') 
{
    return implode('', array_map(function ($item) use ($delimiter) {
        return implode($delimiter, $item) . PHP_EOL;
    }, $array));
}

Here is working demo.

But if you want just write the results to file then @RossWilson answer is better. As fputcsv can properly escape quotes and also can take the $delimeter option and $escape_char option.

And as for why you are getting so many commas in your own code. Firstly, you implode some things twice. Secondly, you are adding the end of line symbol before imploding, so the symbol gets implode with the comma too.