创建单独的CSV';数组中的s(PHP)

创建单独的CSV';数组中的s(PHP),php,arrays,csv,Php,Arrays,Csv,我正在尝试从给定源(即阵列)创建多个csv 它适用于样本数据,但不适用于实际数据 代码如下:- <?php $keycompare = array('98','83'); $arraysource = array ( 0 => array ( 0 => array ( 1 => 'Coogee Tri Club Performance Cycle Jersey Women',

我正在尝试从给定源(即阵列)创建多个csv

它适用于样本数据,但不适用于实际数据

代码如下:-

<?php

$keycompare = array('98','83');
    $arraysource =
    array (
      0 => 
      array (
        0 => 
        array (
          1 => 'Coogee Tri Club Performance Cycle Jersey Women',
          2 => 'WCJ011DC-15924v',
          3 => '2',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        1 => 
        array (
          1 => 'Coogee Tri Club Cycle Bib and Brace Men',
          2 => 'MCB009DC-15925v',
          3 => '6',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        2 => 
        array (
          1 => 'Coogee Tri Club Performance Cycle Jersey Men',
          2 => 'MCJ007DC-15924v',
          3 => '12',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        3 => 
        array (
          1 => 'Coogee Tri Club Performance Triathlon Suit Men/Unisex',
          2 => 'MTU018DC-14792v',
          3 => '5',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        4 => 
        array (
          1 => 'Coogee Tri Club Triathlon Shorts Men',
          2 => 'UTK006DC-14790v',
          3 => '1',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        5 => 
        array (
          1 => 'Coogee Tri Club Run Singlet Men',
          2 => 'MRS004DC-17445v',
          3 => '12',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        6 => 
        array (
          1 => 'Coogee Tri Club Run Singlet Women',
          2 => 'WRT004DC-17445v',
          3 => '5',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        7 => 
        array (
          1 => 'Cogee Tri Club Visor',
          2 => 'BHATDC-17444v',
          3 => '12',
          4 => 'Coogee Triathlon Club - Rd 2',
          5 => '98',
        ),
        8 => 
        array (
          1 => 'Coogee Tri Club Chrlorine Resistant Two Piece Swim Suit Women',
          2 => 'WSU007DC-13856v',
          3 => '3',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        9 => 
        array (
          1 => 'Coogee Tri Club Swim Suit Women',
          2 => 'WSU009DC-13851v',
          3 => '1',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        10 => 
        array (
          1 => 'Coogee Tri Club Elite Triathlon Suit Hydrophobic Women',
          2 => 'WTU022DC-14029v',
          3 => '2',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        11 => 
        array (
          1 => 'Coogee Tri Club Performance Sleeved Triathlon Suit Women',
          2 => 'WTU032DC-14789v',
          3 => '3',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        12 => 
        array (
          1 => 'Coogee Tri Club Performance Sleeved Triathlon Suit Men',
          2 => 'MTU021DC-14789v',
          3 => '3',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
        13 => 
        array (
          1 => 'Coogee Tri Club Cycle Bib and Brace Women',
          2 => 'WCB010DC-15925v',
          3 => '3',
          4 => 'Coogee Triathlon Club - Rd 2,Womens size chart',
          5 => '98',
        ),
        14 => 
        array (
          1 => 'Coogee Tri Club Swim Briefs Men',
          2 => 'MSU001DC-14557v',
          3 => '4',
          4 => 'Coogee Triathlon Club - Rd 2,Mens size chart',
          5 => '98',
        ),
      ),
      1 => 
      array (
        15 => 
        array (
          1 => 'Redcliffe Triathlon Club Triathlon Jersey Womens',
          2 => 'WTJ002DC-16580v',
          3 => '4',
          4 => 'Private Shops,Redcliffe Triathlon Club,Womens size chart',
          5 => '83',
        ),
        16 => 
        array (
          1 => 'Redcliffe Triathlon Club Triathlon Knicks Mens',
          2 => 'UTK006DC-14316v',
          3 => '3',
          4 => 'Private Shops,Redcliffe Triathlon Club,Mens size chart',
          5 => '83',
        ),
        17 => 
        array (
          1 => 'Redcliffe Triathlon Club Triathlon Shorts Women',
          2 => 'WTK005DC-14316v',
          3 => '2',
          4 => 'Private Shops,Redcliffe Triathlon Club,Womens size chart',
          5 => '83',
        ),
      ),
    );
    
    // some data to be used in the csv files
    $headers = array('id', 'name', 'age', 'species');
    $records = array(
        array('1', 'gise', '4', 'cat'),
        array('2', 'hek2mgl', '36', 'human')
    );
    
    // create your zip file
    $zipname = 'file.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    
    // loop to create 3 csv files
    for ($i = 0; $i < 3; $i++) {
    
        // create a temporary file
        $fd = fopen('php://temp/maxmemory:1048576', 'w');
        if (false === $fd) {
            die('Failed to create temporary file');
        }
        
        // write the data to csv
        fputcsv($fd, $headers);
        foreach($arraysource as $record) {
            fputcsv($fd, $record);
        }
    
        // return to the start of the stream
        rewind($fd);
         
        // add the in-memory file to the archive, giving a name
        $zip->addFromString('file-'.$i.'.csv', stream_get_contents($fd) );
        //close the file
        fclose($fd);
    }
    // close the archive
    $zip->close();

脚本有一些问题,我改变了它在数据上循环的方式,现在它使用
foreach
循环在
$arraysource
上循环。然后,对于每个循环,它将该数据集输出到CSV文件。我还更改了文件名以包含
$keycompare
中的相应值,因此文件名更相关。您可以将
$headers
中的值更改为字段的实际名称,这些只是为了演示如何操作

// Change this next line to be the column headings
$headers = array('Description', 'Code', 'Size', 'Something Else', 'value');
// create your zip file
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

// Loop over the data to output
foreach ( $arraysource as $index => $csvData )  {
    
    // create a temporary file
    $fd = fopen('php://temp/maxmemory:1048576', 'w');
    if (false === $fd) {
        die('Failed to create temporary file');
    }
    
    // write the data to csv
    fputcsv($fd, $headers);
    // Output the data for the current file
    foreach($csvData as $record) {
        fputcsv($fd, $record);
    }
    
    // return to the start of the stream
    rewind($fd);
    
    // add the in-memory file to the archive, giving a name (including the ID)
    $zip->addFromString('file-'.$keycompare[$index].'.csv', stream_get_contents($fd) );
    //close the file
    fclose($fd);
}
// close the archive
$zip->close();

这提供了一个包含两个文件的zip文件,
file-98.csv
file-83.csv
脚本有一些问题,我已经改变了它在数据上循环的方式,现在它使用
foreach
循环在
$arrarysource
上循环。然后,对于每个循环,它将该数据集输出到CSV文件。我还更改了文件名以包含
$keycompare
中的相应值,因此文件名更相关。您可以将
$headers
中的值更改为字段的实际名称,这些只是为了演示如何操作

// Change this next line to be the column headings
$headers = array('Description', 'Code', 'Size', 'Something Else', 'value');
// create your zip file
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

// Loop over the data to output
foreach ( $arraysource as $index => $csvData )  {
    
    // create a temporary file
    $fd = fopen('php://temp/maxmemory:1048576', 'w');
    if (false === $fd) {
        die('Failed to create temporary file');
    }
    
    // write the data to csv
    fputcsv($fd, $headers);
    // Output the data for the current file
    foreach($csvData as $record) {
        fputcsv($fd, $record);
    }
    
    // return to the start of the stream
    rewind($fd);
    
    // add the in-memory file to the archive, giving a name (including the ID)
    $zip->addFromString('file-'.$keycompare[$index].'.csv', stream_get_contents($fd) );
    //close the file
    fclose($fd);
}
// close the archive
$zip->close();

这提供了一个包含两个文件的zip文件,
file-98.csv
file-83.csv

任何提示都将不胜感激。谢谢这看起来很像@berend,是的(我投票决定结束这个问题),但至少这也显示了更多的解决问题的尝试。任何提示都将不胜感激。谢谢这看起来很像@berend,是的(我投票决定结束这个问题),但至少这也显示了更多的解决问题的尝试。非常感谢你的回答和解释。我有一个问题,我将获取csv并将其作为电子邮件附件发送。根据我的理解,我认为电子邮件发送的代码将在“fputcsv($fd$record)”之后循环,对吗?@newphpdev,取决于您发送的内容。如果发送CSV文件或zip文件。直到
$zip->close()
.Ohps之后,zip文件才会完全创建,那么我不太确定如何从zip获取文件并将其与此循环一起发送。有什么想法吗?在循环的最后,应该有一个名为
file.zip
的文件,然后您应该能够附加它(使用类似于帮助的东西)。(我想我只需要为发送邮件创建一个函数并将其传递到这个循环中(即foreach($arraysource as$index=>$csvData){)&使其工作。非常感谢您的回答和解释。我有一个问题,我将获取csv并将其作为电子邮件附件发送。我认为根据我的理解,电子邮件发送的代码将在“fputcsv($fd,$record)”之后循环是否正确?@newphpdev,取决于您发送的内容。如果您发送CSV文件或zip文件,则在
$zip->close()之后才会完全创建zip文件
.Ohps,那么我不太确定如何从zip获取文件并将其与此循环一起发送。有什么想法吗?在循环结束时,应该有一个名为
file.zip
的文件,然后您应该能够附加此文件(使用类似于帮助的东西)。(我想我只需要创建一个发送邮件的函数并将其传递到此循环中。)(即foreach($arraysource as$index=>$csvData){)&让它工作起来。