【问题标题】:Array into a CSV file using same id in PHP在 PHP 中使用相同的 id 将数组放入 CSV 文件
【发布时间】:2018-11-13 13:10:55
【问题描述】:

我有一个名为:$messages 的数组,其中包含此数据。 (动态生成)。

如何为每个 txt_id 创建一个包含 3 列(txt、时间和名称)的 CSV 文件?

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

所以结果应该是 Messages1.csv 和 Messages2.csv 因为我们有 2 个不同的 txt_id。

并且 csv 文件的输出应该是 Messages1.csv :

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

对于 Messages2.csv :

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

我尝试使用此代码。

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

这段代码的结果是一个大的 csv 文件,其中包含所有对话,但缺少列名,并且标题重复。

例如,我生成了对话,输出为:

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 对不起,我更新了我的帖子。

标签: php arrays csv merge fputcsv


【解决方案1】:

所有 txt_id 在您共享的 csv 数据中都有不同的值。那你怎么可能只想要 2 个基于不同 txt_id 的 csv 文件呢?

是这样吗,txt_id: 3 & 5 应该每次都在 Messages1.csv 中,而其他 txt_id 值详细信息将在 Messages2.csv 中

【讨论】:

  • 数组 1 和 2 具有相同的 txt_id。输入错误。我的错。
  • 那么你想要为每个 txt_id 生成一个单独的 csv 文件吗?
  • 是的,我想要一个为每个 txt_id 动态生成的单独 csv 文件
【解决方案2】:

试试下面的代码:

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

【讨论】:

    【解决方案3】:

    这是另一个例子。它为不同的txt_id 值生成不同的文件。因此,如果您的 txt_id 值多于两个,则可能有两个以上的文件。

    <?php
    // Input values
    $messages = array();
    $messages[] = array(
        "txt"=>"Hey",
        "txt_id"=>"5",
        "name"=>"John Doe",
        "id_user"=>"1",
        "time"=>"2017-12-15 08:28:47"
    );
    $messages[] = array(
        "txt"=>"Hey John",
        "txt_id"=>"5",
        "name"=>"Gilles Ragout",
        "id_user"=>"1",
        "time"=>"2017-12-15 08:37:21"
    );
    $messages[] = array(
        "txt"=>"What ?",
        "txt_id"=>"12",
        "name"=>"Noémie Nail",
        "id_user"=>"56",
        "time"=>"2017-05-10 05:12:36"
    );
    
    // Delimiter and different id's. File creation and columns titles.
    $delimiter = ',';
    $ids = array();
    foreach ($messages as $item) {
        $id = $item['txt_id'];
        if (!array_key_exists($id, $ids)) {
            $ids[$id] = $id;
            $file = fopen('messages'.$ids[$id].'.csv', "w");
            $line = array('txt', 'time', 'name');
            fputcsv($file, $line, $delimiter);
            fclose($file);
        }
    }
    
    // Data
    foreach ($messages as $item) {
        $line = array($item['txt'], $item['time'], $item['name']);
        $id = array_search($item['txt_id'], $ids);
        $file = fopen('messages'.$ids[$id].'.csv', "a");
        fputcsv($file, $line, $delimiter);
        fclose($file);
    }
    
    // End
    echo 'OK.';
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2015-02-03
      • 1970-01-01
      • 2017-09-27
      • 2021-05-30
      相关资源
      最近更新 更多