如何将此数组放入csv或excel?

时间:2014-11-27 10:13:43

标签: php arrays excel csv

我无法找到一种方法来做这个,而且我对使用数组非常不好,所以我希望有人可以帮助我。

我有数组卡住了只包含名字,比如这个

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

我使用sort来按字母顺序排列名称,比如

sort($stuck);

现在问题是我想将这个数组放入csv或excel文件中,因此名字的第一个字母将是标题,所有带有此字母的名称都将在此标题下。

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

enter image description here

4 个答案:

答案 0 :(得分:1)

在这里尝试一下。我在代码之间添加了注释。

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

答案 1 :(得分:1)

<?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',''

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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