Search code examples
phpfputcsv

Php fputcsv trouble


I have a very basic question about fputcsv.

My goal is to create a csv file where for each row the first column is the key and the second the value. The result of print_r on my table gives something like this Array ( [city_name: London ] => 8000 [city_name: Dublin ] => 8415

I thought i had to use array_keys and array_values like this :

fputcsv ($fp, array_keys($tablepeople)); 
fputcsv ($fp, array_values($tablepeople));

With this code my csv file got this shape : Name1 Name2 Value1 Value2

How can I do to get this one: Name1 Value 1 Name2 Value2

Where name is the key of the array and value the value.

Could you help me please ?


Solution

  • You have to create an array for each line in your CSV file.

    $line = array($key, $value);
    fputcsv($out, $line);