2013-10-28 34 views
1

我正在尝试创建一个数据分析门户,该门户允许我将其中一个表导出为CSV。我现在遇到的问题是,当用户选择大量数据时,它会失败,因为它已经达到了我的浏览器可用的Javascript变量的最大堆栈大小。 (如解释here)。解决方案是将它们分成更小的Javascript数组块,然后再将它们连接起来。将大的PHP数组分割为更小的块用于javascript

我的问题

我有一个很大的PHP数组(可变大小的,因为我不知道什么范围的用户选择),我需要将它们分割成JavaScript数组的变量数,然后再次将它们连接起来后来。我怎样才能做到这一点?

我的代码目前看起来像

<?php 
$array2 = getAllRawData($startDate, $endDate, $merchantID); 
array_unshift($array2, ...TO ADD HEADER TO CSV....)); 
?> 

// I am thinking some magic is supposed to happen here that splits my PHP array into smaller parts, assign them to javascript arrays and then concatenate them together into items2. 

var items2 = <?php echo json_encode($array2);?>; // this is what I currently do but fails when the date range gets too wide. 

var jsonObject2 = JSON.stringify(items2); 
var csv2 = ConvertToCSV(jsonObject2); 

a=document.createElement('a'); 
a.textContent='Download Raw Waiting Time Report'; 
a.download="RawWaitTime.csv"; 
a.href='data:text/csv;charset=utf-8,'+escape(csv2); 
document.body.appendChild(a); 
+2

你为什么要经历这一切?你不能直接从服务器提供CSV文件作为下载吗? – 2013-10-28 03:15:44

+1

同意Mike W.通常,当您将PHP代码混合到Javascript代码中时,您会过度复杂化某些内容。 – teynon

+0

嗯,我从来没有想到,也许我会试一试。任何意见或指针,将非常感激! – chongzixin

回答

4

正如评论指出,这可能是最好建在CSV PHP。但是,要回答如何为Javascript分割数组的问题,可以使用窗口容器。但是,这意味着对象可能不具有相同的Javascript限制,或者您链接的帖子不准确。 (我不知道,因为我还没有看到,也没有测试过这个极限误差。)

http://jsfiddle.net/TYwY8/7/

<script> 
var Array0 = ["1", "2", "3"]; 
var Array1 = ["4", "5", "6"]; 
var Array2 = ["7", "8", "9"]; 
var Array3 = ["10", "11", "12"]; 
var Array4 = ["13", "14", "15"]; 
var ArrayCount = 5; 

for (var x = 0; x < ArrayCount; x++) { 
    console.log(window["Array" + x]); 
} 

</script> 

你可以通过切片PHP这些阵列是这样的:(未经测试)

<?php 

    $limit = 10000; 
    $arrayCount = count($array) % limit; 

    $offset = 0; 
    for ($x = 0; $arrayCount; $x++) { 
     echo "var MyArray{$x} = [" . implode(",", array_slice($array, $offset, $limit)) . "];"; 
     $offset += $limit; 
    } 

要使用fputcsv没有写入文件:

<?php 

    $file = fopen("php://output", "w"); 

    $array = [ [ "1", "2", "3" ], [ "4", "5", "6" ], [ "7", "8", "9" ] ]; 

    foreach ($array as $value) { 
     fputcsv($file, $value); 
    } 
+0

非常感谢您的回答。因此,为了允许作为可下载的CSV文件,我应该将上面的fputcsv放到它自己的php文件中,并像这个链接中提到的那样向它添加标题? (http://stackoverflow.com/questions/15769732/write-to-php-output-buffer-and-then-download-csv-from-buffer) – chongzixin

+0

@ user1258600是的。 – teynon

0

Tom很好的回答。以下是另一种可用于生成CSV输出的解决方案:

<?php 
$array2 = getAllRawData($startDate, $endDate, $merchantID); 
// This may or may not be necessary, depending on if you have 
// objects mixed in with the value of $array2 
$array2 = unserialize(serialize(json_decode(json_encode($array2), 1))); 
print create_csv($array2); 

function create_csv($input) { 
    $output = fopen("php://output", 'w'); 
    function create_csv_handler(&$values, $key, $fh) { 
     fputcsv($fh, $values, ',', '"'); 
    } 
    array_walk($input, 'create_csv_handler', $output); 
    fclose($output); 
} 
+0

如果OP按照Javascript堆栈耗尽消息所建议的那样处理大数据集,那么使用该数组并伴随函数调用的很可能会增加显着的滞后时间。 – teynon

相关问题