如何将多维数组写入csv文件.的PHP [英] How to write a multidimensional array to a csv file. PHP

查看:220
本文介绍了如何将多维数组写入csv文件.的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的多维数组 $ csv .

I have a multidimensional array $csvcontaining the following contents.

Array ( [0] => JOHN WILLIAMS [1] => 6/8/1998 [2] => 55456434E [3] => 4321 )
Array ( [0] => SARAH JONES [1] => 15/01/1982 [2] => 56834645Q [3] => 1234 )
Array ( [0] => JAMES BRENNAN [1] => 09/05/1978 [2] => 25689514W [3] => 8575)

我从csv文件中获得了此数组,以更新文件中的元素之一.现在,它已更新,我想将更新的元素添加回新的csv文件.

I got this array from a csv file to update one of the elements within the file. Now that its updated, I want to add the updated elements back to a new csv file.

$csv = array();
$lines = file("persistence/employee_data.csv", FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}

我想将此更新后的数组写入一个新的csv文件,但是您不能将多维数组写入一个csv文件.我想将此数组转换为这样的

I want to write this updated array to a new csv file but you cannot write a multidimensional array to a csv file. I want to convert this array to something like this

Array ( [0] => JOHN WILLIAMS [1] => 6/8/1998 [2] => 55456434E [3] => 4321 )
Array ( [0] => SARAH JONES [1] => 15/01/1982 [2] => 56834645Q [3] => 1234 )
Array ( [0] => JAMES Brennan [1] => 09/05/1978 [2] => 25689514W [3] => 8575 
) 

所以我可以轻松地使用 fputcsv()函数将数组的内容写入csv文件.

so I can easily use the fputcsv()function to write the contents of the array to a csv file.

    $dataSrc = "persistence/employee_data.csv";
   $outFile = fopen($dataSrc, "w") or die("Unable to open file!");
   fputcsv($outFile, $csv);
   fclose($outFile);

注意,我知道如何创建和写入新文件,我只需要多维数组问题的帮助,即可将其转换为可以轻松写入csv文件的内容.

Note I know how to create and write to a new file, I just need help on the multidimensional array problem converting it into something that I can easily write to a csv file.

csv文件中的格式如下(不包括附件).

the format within the csv file will look like this (not including the enclosures).

JANE WILLIAMS,6/8/1998,55846874E,4321 
SARAH JONES,15/01/1982,56897547Q,1234
JAMES BRENNAN,09/05/1978,25689514W,8575

做到这一点的最佳方法是什么?

What would be the best approach to do this?

编辑

对于每个语句,只需简单地使用a.

Just simply used a for each statement lol.

   $dataDest = "persistence/employee_data.csv";
   $outFile = fopen($dataDest, "w") or die("Unable to open file!");

   foreach($csv as $value){

       fputcsv($outFile, $value);

   }

fclose($outFile);

推荐答案

如您所见, fputcsv() 将一维数组作为输入,并将每个元素保存在一行中.

As you figured, fputcsv() takes a unidimensional array as input, and saves each element in one row.

要将每个数组保存在数组中,请遍历父数组并按通常使用 fputcsv()保存每个子数组:

To save each array in an array, loop through the parent array and save each subarray as you would normally with fputcsv():

<?php
$data[] = ["JANE WILLIAMS", "6/8/1998", "55846874E", "4321"];
$data[] = ["SARAH JONES", "15/01/1982", "56897547Q", "1234"];
$data[] = ["JAMES BRENNAN", "09/05/1978", "25689514W", "8575"];

$outFile = fopen("data.csv", "w") or die("Unable to open file!");
foreach ($data as $row) {
    fputcsv($outFile, $row);
}
fclose($outFile);

这篇关于如何将多维数组写入csv文件.的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆