如何把这个数组放入CSV或Excel?

我想不出有办法做到这一点,我真的很难与数组,所以我希望有人能帮助我。

我有数组$卡住,只是包含名称,就像这样

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

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

 sort($stuck); 

现在的事情是我想把这个数组放入一个CSV或Excel文件,所以名字的第一个字母将是标题,所有与这个字母的名字将在这个标题下。

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

在这里输入图像说明

这里试试这个。 我在代码之间添加了评论。

 $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; 
 <?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','' 
 <?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); 

这是我在这个问题上的尝试:

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