如何使用 PHP 将数组拆分为多个 csv 文件

How to split array to multiple csv files using PHP

求助!

我想让这部分代码更短,更具变革性。我的 $named 数组看起来像:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Header
   
                )

            [1] => Array
                (
                    [0] => some text
                    [1] => some text

                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Header
                )

            [1] => Array
                (
                    [0] => 
                    [1] => some text
                )

            [2] => Array
                (
                    [0] => some text
                )
        )
 )

所以,如果“header”的数量超过 100,我想保存到一个新的 csv 文件。如何打开和关闭具有正确索引的文件而无需手动键入“/ named_1”或“/named_2”?目前我有一个 y 变量,它遍历 $named 数组并写入文件,直到有 100 headers。我怎样才能不重复代码?

另外每行必须有 38 个分号。


$file = fopen(__DIR__ . '/named_1.csv', 'w');
$file_2 = fopen(__DIR__ . '/named_2.csv', 'w');
$file_3 = fopen(__DIR__ . '/named_3.csv', 'w');
$y=0;
foreach ($named as $array) {
    foreach($array as $row){
        if($y < 100){
            $line = '';
            for ($i=0; $i <= 38; $i++) { 
                if (count($row) > $i) {
                    $line .= $row[$i];
                    ($i != 38 ? $line .= ';' : '');
                } else {
                    ($i != 38 ? $line .= ';' : '');
                }
            }
            fwrite($file, $line.PHP_EOL); 
        }
        if($y > 99 && $y <200){
            $line = '';
            for ($i=0; $i <= 38; $i++) { 
                if (count($row) > $i) {
                    $line .= $row[$i];
                    ($i != 38 ? $line .= ';' : '');
                } else {
                    ($i != 38 ? $line .= ';' : '');
                }
            }
            fwrite($file_2, $line.PHP_EOL); 
        }
        if($y > 199 && $y <300){
            $line = '';
            for ($i=0; $i <= 38; $i++) { 
                if (count($row) > $i) {
                    $line .= $row[$i];
                    ($i != 38 ? $line .= ';' : '');
                } else {
                    ($i != 38 ? $line .= ';' : '');
                }
            }
            fwrite($file_3, $line.PHP_EOL); 
        }
    }
    $y++;
}
fclose($file);
touch( __DIR__ . '/named_1.csv');
fclose($file_2);
touch( __DIR__ . '/named_2.csv');
fclose($file_3);
touch( __DIR__ . '/named_3.csv');

请尝试以下代码。我添加了一个为不同需求编写文件名的功能。

function composeFilename($k, $mod, $type=1)
{
    switch($type)
    {
        // for named_1-100.csv, named_101-200.csv, ...
        case 1:
            $from = $k - $mod + 2;
            $to = $k+1;
            $suffix = $from . "_" .$to;
            break;
        // for named_0-99.csv, named_100-199.csv, ...
        case 2:
            $from = $k - $mod + 1;
            $to = $k;
            $suffix = $from . "_" .$to;
            break;
        // for named_1.csv, named_2.csv, ...
        default:
            $suffix = ($k % $mod)+1;
    }
    return "named_$suffix.csv";
}

$lines = [];
$mod = 100;// The max number of rows in each output file
$outputDirectory = __DIR__ . "/";
$lastIndex = count($named) - 1;
foreach ($named as $k => $array){
    foreach($array as $row){
        $lines[] = trim(implode(";", $row));
    }

    // Write output file for every $mod lines or for the rest
    if(($k+1) % $mod == 0 || $k == $lastIndex){
        $filename = $outputDirectory . composeFilename($k, $mod);
        if(file_put_contents($filename, implode(PHP_EOL, $lines))){
            // Reset lines array for the next chunk
            $lines = [];
        }
        else {
            die("$filename could not be saved.");
        }
    }
}