dongluyi5123 2013-10-28 03:08
浏览 44
已采纳

将大PHP数组拆分为javascript的较小块

I am trying to create a data analytics portal which allows me to export one of my tables as CSV. The problem I have now is that when a user selects a huge range of data, it fails because it has reached the maximum stack size for Javascript variables available to my browser. (As explained here). The solution is to split them into smaller Javascript array chunks and then concatenate them again later.

My problem

I have a big PHP array (of variable size because I wont know what range the user selects) and I need to split them into a variable number of Javascript arrays and then concatenate them again later. How can I achieve that?

My code currently looks like that

<?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条回答 默认 最新

  • doupo1890 2013-10-28 03:36
    关注

    As stated in comments, it is probably best to build the CSV in PHP. However, to answer the question of how to split the arrays for Javascript, you could use the window container. However, this implies that objects may not have the same Javascript limit or that the post you link to is inaccurate. (I'm not sure because I haven't seen nor tested this limit error.)

    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>
    

    You could slice those arrays via PHP like this: (untested)

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

    To use fputcsv without writing a file:

    <?php
    
        $file = fopen("php://output", "w");
    
        $array = [  [ "1", "2", "3" ], [ "4", "5", "6" ], [ "7", "8", "9" ] ];
    
        foreach ($array as $value) {
            fputcsv($file, $value);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟