【问题标题】:How do I put this array into csv or excel?如何将此数组放入 csv 或 excel 中?
【发布时间】:2014-11-27 10:13:43
【问题描述】:

我想不出一种方法来做到这一点,而且我在使用数组方面真的很糟糕,所以我希望有人可以帮助我。

我有一个只包含名称的数组 $stuck,就像这样

$stuck = array('Daniel','Alex','alfredo','dina');

我正在使用 sort 将名称按字母顺序排列,就像这样

sort($stuck);

现在我想把这个数组放到一个 csv 或 excel 文件中,这样名字的第一个字母就是标题,所有带有这个字母的名字都在这个标题下。

这是我想要完成的一个例子

【问题讨论】:

  • 你应该把你的数组格式设置为 $stuck=(array("title"=>"A","Alex","Alfredo"),array("title"=>"B ","拜娜"));然后使用此处描述的导出the-art-of-web.com/php/dataexport

标签: php arrays excel csv


【解决方案1】:

来试试这个。我在代码之间添加了 cmets。

$csvArray = array();
$nameArray = array('Daniel','Alex','alfredo','dina');
//Create an array that can be used for creating the CSV
foreach($nameArray as $name)
{
    $firstLetter = strtoupper(substr($name, 0,1));
    $csvArray[$firstLetter][] = $name; 
}

//Determine the subarray which have the must rules
$maxRuleCount = 0;
foreach($csvArray as $firstLetter => $nameArray)
{
    if(count($nameArray) > $maxRuleCount)
    {
        $maxRuleCount = count($nameArray);
    }
}

//Create the first rule (letters)
$csvContent = implode(';', array_keys($csvArray));      
$csvContent .= "\r\n";

//Loop through the CSV array and create rules of the names
for($i = 0 ; $i < $maxRuleCount ; $i++)
{       
    foreach($csvArray as $firstLetter => $nameArray)
    {
        $csvContent .= @$csvArray[$firstLetter][$i].';';
    }
    $csvContent .= "\r\n";
}

//The hole csv content is in the $csvContent variable now
//If you want to print it you need to add the text/plain header because of the \r\n new line characters
header("Content-Type:text/plain");      
echo $csvContent;

输出是:

D;A
Daniel;Alex;
dina;alfredo;   

【讨论】:

    【解决方案2】:
    <?php
    
    $stuck = array('Daniel','Alex','Dina','durban','eigor','alfredo','dina');
    
    // associate names with first letters in a new array
    foreach ($stuck as $name) {
        $first_letter = strtoupper( substr( $name, 0, 1 ) );
        $new_name_array[ $first_letter ][] = $name;
    }
    
    // store the first letter list in an array and sort it
    $first_letters = array_keys( $new_name_array );
    sort( $first_letters );
    
    
    // sort the name lists
    foreach( array_keys( $new_name_array ) as $letter ) {
        sort( $new_name_array[$letter] );
    }
    
    // output csv header row
    echo "'".implode( "','", $first_letters )."'\n";
    
    // output the CSV name rows
    $i = 0;
    while ( true ) {
        $row = array();
        $is_row = false;
        foreach( $first_letters as $letter ) {
            $row[] = $new_name_array[ $letter ][ $i ];
            if( ! empty( $new_name_array[ $letter ][ $i ] ) ) $is_row = true;
        }
        if( ! $is_row ) exit;
        echo "'" . implode( "','", $row ) . "'\n";
        $i++;
    }
    
    ?>
    

    输出引用的 CSV:

    'A','D','E'
    'Alex','Daniel','eigor'
    'alfredo','Dina',''
    '','dina',''
    '','durban',''
    

    【讨论】:

    • 这也是可行的,我不知道哪个选项更好,但我要感谢你帮助我,所以谢谢你!!!!
    【解决方案3】:
    <?php
    
    $stuck = array('Daniel','Alex','alfredo','dina', 'eigor', 'durban');
    sort($stuck);
    $sortedNames = array();
    $maxEl = 0;
    foreach ($stuck as $name) {
      $name = strtolower($name);
      $sortedNames[$name{0}][] = $name;
      $maxEl = count($sortedNames[$name{0}]) < $maxEl ? $maxEl : count($sortedNames[$name{0}]);
    }
    $headers = array_keys($sortedNames);
    $finalArray = array($headers);
    for($i = 0; $i < $maxEl; $i++) {
      $tmpRow = array();
      foreach ($sortedNames as $names) {
        $tmpRow[] = isset($names[$i]) ? $names[$i] : '';
      }
      $finalArray[] = $tmpRow;
    }
    
    $file = fopen("names.csv","w");
    
    foreach ($finalArray as $line)
    {
      fputcsv($file,$line);
    }
    fclose($file);
    

    【讨论】:

    • 首先我要感谢您,真的非常感谢!其次,我想问一个愚蠢的问题,我该如何下载?我想我需要制作类似这样的标题(“Content-Disposition: attachment; filename=\"names.csv\""); ?
    • header("Content-type: text/csv"); header("内容配置:附件;文件名=names.csv"); header("Pragma: no-cache"); header("过期时间:0"); echo file_get_contents("names.csv");
    • 这确实下载了文件,但在此之前我用 ftp 查看了文件,由于某种原因它只是空的,你知道为什么吗?
    【解决方案4】:

    这是我对这个问题的尝试:

    $names = array('Daniel','Alex','alfredo','dina', 'eigor', 'falfoul', 'fiona');
    natcasesort($names);
    
    $columns = array();
    foreach ($names as $name) {
        $first = strtoupper(substr($name, 0, 1));
        $columns[$first][] = $name;
    }
    
    // pad the columns so they all have the same number of rows
    $maxRows = 0;
    foreach ($columns as &$col) {
        $numRows = count($col); 
        if ( $numRows > $maxRows) {
            $maxRows = $numRows; 
        }
    }
    
    // pad the columns
    foreach ($columns as &$column) {
        $column = array_pad($column, $maxRows, '');
    }
    
    $firstLetter = array_keys($columns);
    $numCols = count($firstLetter);
    
    // transpose the columns array
    $rows = array();
    foreach ($columns as $csvCol) {
        foreach ($csvCol as $k=>$n) {
            $rows[$k][] = $n;
        }
    }
    
    // pad the rows to ensure they all have the same number of
    // columns
    foreach ($rows as &$row) {
        $row = array_pad($row, $numCols, '');
    }
    
    // add the title row to the rows
    array_unshift($rows, $firstLetter);
    
    $handle = fopen("names.csv", 'w');
    foreach ($rows as $fRow) {
        fputcsv($handle, $fRow);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2019-03-30
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多