Hello guys just want to ask how can I put an associative array in csv? For example if I have an array like this.
Array
(
    [0] => Array
        (
            [restaurant_id] => 1227
            [new_lat] => 13.62241
            [new_long] => 123.19341
            [date_updated] => 2013-11-14 11:20:26
        )
    [1] => Array
        (
            [restaurant_id] => 1218
            [new_lat] => 14.66732
            [new_long] => 121.02618
            [date_updated] => 2013-11-14 11:22:22
        )
)
For my code in generating csv is this:
            $restaurant_id = $post_data['company_id'];
            $new_lat_entry = $post_data['new_lat'];
            $new_long_entry = $post_data['new_long'];
            $data_add =  array(
                'restaurant_id' => $restaurant_id,
                'new_lat' => $new_lat_entry,
                'new_long' => $new_long_entry,
                'date_updated' => date('Y-m-d H:i:s')
            );
            $data = unserialize(file_get_contents('addresses.txt'));
            $data[] = $data_add;
            $serialize_data = serialize($data);
            file_put_contents("addresses.txt", $serialize_data, LOCK_EX); //write the text file
            $array = unserialize(file_get_contents('addresses.txt')); //THIS WILL GET THE ARRAY
                    echo "<pre>";
            print_r($array); //display it
            $csv = '';
            foreach($array as $row) {
                $csv .= implode(',', $row) . "\n";
            }
            //fn_print_die($csv);
            $file_input = fopen("addresses.csv","w");
            foreach($csv as $line){
                fputcsv($file_input,split(',',$line));
            }
            fclose($file_input);
                Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.
$_POST is a predefined variable which is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.
To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file.
This should work...
 <?php
  foreach ($array as $row) {
       fputcsv($file_input, $row);
 }
 fclose($file_input);
  ?>
Just refer to the fputcsv manual on php.net
You should try to implement SPL classes when possible:
$csv_file = new SplFileObject('addresses.csv', 'w');
foreach ($address_list as $address_fields) {
    $csv_file->fputcsv($address_fields);
}
                        You can use implode to do something like this pretty easily
$csv = '';
foreach($array as $row) {
    $csv .= implode(',', $row) . "\n";
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With