处理对于 json_encode (PHP) 来说太大的数据

Handling data that is too large for json_encode (PHP)

我正在尝试通过 ajax 在我的 symfony 项目中获取一些数据库数据。我抓取了我们数据库中的所有估计值,超过 60k。当我尝试 json_encode 结果时,出现内存不足错误。我知道它与大小有关,因为我可以 json_encode 一个结果并且它 returns 很好。

$results = new \stdClass;
$results->jobs = $epms->getEstimates();

// JSON encode results to send to the view. The view just echos them out for jQuery to process.
$json_encoded = json_encode($results->jobs[1]); //Works
$json_encoded = json_encode($results->jobs); //Doesn't work
$headers = array(
    'Content-Type' => 'application/json'
);
$response = new Response($json_encoded, 200, $headers);
return $response;

我是一个 PHP 菜鸟,但我的想法是拆分结果并对较小的部分进行编码,然后我可以在返回我的 ajax 调用之前将它们连接在一起。最好的方法是什么?我不想增加我的 php 内存限制。

您需要动态增加内存限制:-

ini_set('memory_limit','16M');// change 16M to your desired number

注:-

以上代码会将 PHP 可用的最大内存量增加到 16 MB,此设置仅针对 运行 脚本进行了调整。