Search code examples
phparrayscsvmergefputcsv

Array into a CSV file using same id in PHP


I have an array called : $messages with this data. (Generated dynamically).

How can I create a CSV file which contains 3 columns (txt, time and name) for each txt_id with the values ?

array(3) {
  [0]=>
  array(5) {
    ["txt"]=>
    string(3) "Hey"
    ["txt_id"]=>
    string(1) "5"
    ["name"]=>
    string(8) "John Doe"
    ["id_user"]=>
    string(1) "5"
    ["time"]=>
    string(19) "2017-12-15 08:28:47"
  }
  [1]=>
  array(5) {
    ["txt"]=>
    string(8) "Hey John"
    ["txt_id"]=>
    string(1) "5"
    ["name"]=>
    string(13) "Gilles Ragout"
    ["id_user"]=>
    string(1) "58"
    ["time"]=>
    string(19) "2017-12-15 08:37:21"
  }
 [2]=>
 array(5) {
   ["txt"]=>
   string(6) "What ?"
   ["txt_id"]=>
   string(2) "12"
   ["name"]=>
   string(11) "Noémie Nail"
   ["id_user"]=>
   string(1) "56"
   ["time"]=>
   string(19) "2017-05-10 05:12:36"
 }

So the result should be Messages1.csv and Messages2.csv because we have 2 differents txt_id.

And the output of the csv file should be for Messages1.csv :

txt            | time                | name
Hey              2017-12-15 08:28:47  John Doe
Hey John         2017-12-15 08:37:21  Gilles Ragout

And for Messages2.csv :

txt            | time                | name
What ?           2017-05-10 05:12:36   Noemie Nail

I try with this code.

$file = fopen('php://temp', 'w');
if (false === $file) {
    die('Failed to create temporary file');
}
foreach ($getUserConv as $conv) {
    fputcsv($file, array_keys($conv));
    fputcsv($file, $conv);
}
rewind($file);
$zip->addFromString(__('Conversations').'.csv', stream_get_contents($file));
fclose($file);

And the result of this code is a large csv file which contains all conversation but the column name is missing, and the headers is repeated.

For example I have Conversations which is generated and the output is :

txt | time
Hi    2017-12-18 01:01:10
txt | time
Yo    2017-12-18 02:10:08    

Solution

  • Try below code:

    $messages = array(array('txt' => 'Hey', 'txt_id' => '5', 'name' => 'John Doe', 'id_user' => '5', 'time' => '2017-12-15 08:28:47'),
                          array('txt' => 'Hey John', 'txt_id' => '5', 'name' => 'Gilles Ragout', 'id_user' => '5', 'time' => '2017-12-15 08:37:21'),
                          array('txt' => 'What ?', 'txt_id' => '12', 'name' => 'Noemie Nail', 'id_user' => '56', 'time' => '2017-05-10 05:12:36'));
        echo '<br>messages=<pre>'. print_r($messages, true) .'</pre><br>';
        $csv_data = array();
        foreach($messages as $key => $value) {
            $csv_data[$value['txt_id']][] = array('txt' => $value['txt'], 'time' => $value['time'], 'name' => $value['name']);
        }
        unset($key, $value);
    
        if( is_array($csv_data) && count($csv_data) > 0 ) {
            foreach($csv_data as $index => $data) {
                if( is_array($data) && count($data) > 0 ) {
                    $file = fopen('/tmp/Messages'. $index .'.csv', 'w');
                    fputcsv($file, array_keys($data[0]));
                    foreach($data as $key => $value) {
                        fputcsv($file, $value);
                    }
                    fclose($file);
                    unset($key, $value, $file);
                }
            }
            unset($index, $data);
        }