Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put associative array in csv using PHP?

Tags:

arrays

php

csv

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);
like image 653
Jerielle Avatar asked Nov 14 '13 03:11

Jerielle


People also ask

What is an associative array in PHP give example?

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.

Is $_ POST an associative array?

$_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.

How do I create a CSV file in PHP?

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.


3 Answers

This should work...

 <?php

  foreach ($array as $row) {
       fputcsv($file_input, $row);
 }

 fclose($file_input);

  ?>

Just refer to the fputcsv manual on php.net

like image 170
James Avatar answered Nov 13 '22 09:11

James


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);
}
like image 41
Anthony Avatar answered Nov 13 '22 09:11

Anthony


You can use implode to do something like this pretty easily

$csv = '';
foreach($array as $row) {
    $csv .= implode(',', $row) . "\n";
}
like image 22
Machavity Avatar answered Nov 13 '22 09:11

Machavity