【问题标题】:splitting big PHP array into smaller chunks for javascript将大 PHP 数组拆分为较小的块用于 javascript
【发布时间】:2013-11-06 17:49:20
【问题描述】:

我正在尝试创建一个数据分析门户,它允许我将我的一个表导出为 CSV。我现在遇到的问题是,当用户选择大量数据时,它会失败,因为它已达到浏览器可用的 Javascript 变量的最大堆栈大小。 (正如here 所解释的那样)。解决方案是将它们拆分为更小的 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);

【问题讨论】:

  • 你为什么要经历这一切?您不能直接从服务器提供 CSV 文件作为下载文件吗?
  • 同意 Mike W。通常当你将 PHP 代码混合到 Javascript 代码中时,你会过度复杂化。
  • 嗯,我从来没有想过,也许我会尝试一下。任何有关这方面的建议或指示将不胜感激!
  • 我找到了一些说明使用fputcsv 的资源,但恐怕我没有准备好访问我的服务器以允许PHP 写入文件的权限。有什么解决办法吗?
  • @user1258600 如何上传PHP文件?

标签: javascript php arrays


【解决方案1】:

如 cmets 中所述,最好用 PHP 构建 CSV。但是,要回答如何为 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);
    }

【讨论】:

  • 非常感谢您的回答。因此,为了允许将其作为可下载的 CSV,我应该将上面的 fputcsv 放入它自己的 php 文件中,并像这个链接中提到的那样向它添加标题? (stackoverflow.com/questions/15769732/…)
【解决方案2】:

汤姆的回答很好。这是另一个也适用于生成 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);
}

【讨论】:

  • 如果 OP 正在处理与 Javascript 堆栈耗尽消息所建议的一样大的数据集,则很有可能使用带有函数调用的数组遍历会增加明显的延迟时间。
猜你喜欢
  • 2021-03-28
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2018-03-23
  • 2020-01-14
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多