gpt4 book ai didi

PHP - 按列数组到 CSV

转载 作者:行者123 更新时间:2023-12-04 02:23:00 24 4
gpt4 key购买 nike

问题:我有一个关联数组,其中所有键代表 csv 标题,每个 $key => 数组中的值.. 代表该列中的项目。

研究:据我所知,fputcsv 喜欢逐行进入,但这种基于列的数组让事情变得复杂。我还没有找到任何可以实现此目的的功能。

示例:

Array(
['fruits'] => Array(
[0] => 'apples',
[1] => 'oranges',
[2] => 'bananas'
),
['meats'] => Array(
[0] => 'porkchop',
[1] => 'chicken',
[2] => 'salami',
[3] => 'rabbit'
),
)

需要成为:

fruits,meats
apples,porkchop
oranges,chicken
bananas,salami
,rabbit

为什么难:

您需要知道最大行数才能制作空白点。

最佳答案

我必须编写自己的函数。认为它可能对其他人有帮助!

/*
* The array is associative, where the keys are headers
* and the values are the items in that column.
*
* Because the array is by column, this function is probably costly.
* Consider a different layout for your array and use a better function.
*
* @param $array array The array to convert to csv.
* @param $file string of the path to write the file.
* @param $delimeter string a character to act as glue.
* @param $enclosure string a character to wrap around text that contains the delimeter
* @param $escape string a character to escape the enclosure character.
* @return mixed int|boolean result of file_put_contents.
*/

function array_to_csv($array, $file, $delimeter = ',', $enclosure = '"', $escape = '\\'){
$max_rows = get_max_array_values($array);
$row_array = array();
$content = '';
foreach ($array as $header => $values) {
$row_array[0][] = $header;
$count = count($values);
for ($c = 1; $c <= $count; $c++){
$value = $values[$c - 1];
$value = preg_replace('#"#', $escape.'"', $value);
$put_value = (preg_match("#$delimeter#", $value)) ? $enclosure.$value.$enclosure : $value;
$row_array[$c][] = $put_value;
}
// catch extra rows that need to be blank
for (; $c <= $max_rows; $c++) {
$row_array[$c][] = '';
}
}
foreach ($row_array as $cur_row) {
$content .= implode($delimeter,$cur_row)."\n";
}
return file_put_contents($file, $content);
}

还有这个:

/*
* Get maximum number of values in the entire array.
*/
function get_max_array_values($array){
$max_rows = 0;
foreach ($array as $cur_array) {
$cur_count = count($cur_array);
$max_rows = ($max_rows < $cur_count) ? $cur_count : $max_rows;
}
return $max_rows;
}

新方法(使用类)

我稍后为此编写了一个类,现在我将提供给任何正在寻找类的人:

class CSVService {

protected $csvSyntax;

public function __construct()
{
return $this;
}

public function renderCSV($contents, $filename = 'data.csv')
{
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');

echo $contents;
}

public function CSVtoArray($filename = '', $delimiter = ',') {
if (!file_exists($filename) || !is_readable($filename)) {
return false;
}

$headers = null;
$data = array();
if (($handle = fopen($filename, 'r')) !== false) {
while (($row = fgetcsv($handle, 0, $delimiter, '"')) !== false) {
if (!$headers) {
$headers = $row;
array_walk($headers, 'trim');
$headers = array_unique($headers);
} else {
for ($i = 0, $j = count($headers); $i < $j; ++$i) {
$row[$i] = trim($row[$i]);
if (empty($row[$i]) && !isset($data[trim($headers[$i])])) {
$data[trim($headers[$i])] = array();
} else if (empty($row[$i])) {
continue;
} else {
$data[trim($headers[$i])][] = stripcslashes($row[$i]);
}
}
}
}
fclose($handle);
}
return $data;
}

protected function getMaxArrayValues($array)
{
return array_reduce($array, function($carry, $item){
return ($carry > $c = count($item)) ? $carry : $c;
}, 0);
}

private function getCSVHeaders($array)
{
return array_reduce(
array_keys($array),
function($carry, $item) {
return $carry . $this->prepareCSVValue($item) . $this->csvSyntax->delimiter;
}, '') . "\n";
}

private function prepareCSVValue($value, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$valueEscaped = preg_replace('#"#', $escape . '"', $value);
return (preg_match("#$delimiter#", $valueEscaped)) ?
$enclosure . $valueEscaped . $enclosure : $valueEscaped;
}

private function setUpCSVSyntax($delimiter, $enclosure, $escape)
{
$this->csvSyntax = (object) [
'delimiter' => $delimiter,
'enclosure' => $enclosure,
'escape' => $escape,
];
}

private function getCSVRows($array)
{
$n = $this->getMaxArrayValues($array);
$even = array_values(
array_map(function($columnArray) use ($n) {
for ($i = count($columnArray); $i <= $n; $i++) {
$columnArray[] = '';
}
return $columnArray;
}, $array)
);

$rowString = '';

for ($row = 0; $row < $n; $row++) {
for ($col = 0; $col < count($even); $col++) {
$value = $even[$col][$row];
$rowString .=
$this->prepareCSVValue($value) .
$this->csvSyntax->delimiter;
}
$rowString .= "\n";
}

return $rowString;
}

public function arrayToCSV($array, $delimiter = ',', $enclosure = '"', $escape = '\\', $headers = true) {
$this->setUpCSVSyntax($delimiter, $enclosure, $escape);

$headersString = ($headers) ? $this->getCSVHeaders($array) : '';

$rowsString = $this->getCSVRows($array);


return $headersString . $rowsString;
}

}

关于PHP - 按列数组到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com