简体   繁体   中英

How to put associative array in csv using PHP?

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);

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";
}

To make this clean, you would have to

  1. collect all keys from the array (foreach twice)
  2. build a second array with each restaurant (no subnodes)
  3. merge the keys array into that and THEN
  4. merge all restaurant subnodes to those keys.

The reason is simple: restaurant A has "[self_service] => true" The CSV line for restaurant B will either miss a column or worse, have "[vegetarian_food] => false" instead.

In the end, you would have "true" and "false" in the same column under each other, with theo first meaning "Yes, self service" and the second meaning "Sorry veggies". You will probably never notice, because they might still have the same count(array).

(I would write this as a comment, but my Stack Exchange reputation does not allow me that yet (48 now, 2 more to go, yay!)

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