doz95923 2016-04-28 17:41
浏览 62
已采纳

PHP循环中允许的内存大小耗尽

I have the following code:

 15 $users = pg_fetch_all($result);
 16 $chart_data = array();
 17 foreach ($users as $value)
 18 {
 19         //var_dump($value);
 20         $temp = array();
 21         $temp['label'] = $value['id'];
 22         $temp['y'] = $value['sum'];
 23 print_r($temp);
 24 /*
 25 echo "<pre>";
 26         var_dump($temp);
 27 echo "</pre>";
 28 */      
 29         array_push($chart_data,$temp);
 30 }
 31 echo count($chart_data);

When I run my code, I get the following error message:

[Thu Apr 28 17:23:39.278844 2016] [:error] [pid 24321] [client 10.63.8.104:58362] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/www/localhost/htdocs/audit/index.php on line 21

This is a sample of the output I get from the print_r():

Array ( [label] => 12 [y] => 0.0021 ) 

I can't see how line 21 is problematic here. Maybe I need to explicitly reset temp to an empty array?? Not sure too but any suggestions welcome. Thanks.

  • 写回答

3条回答 默认 最新

  • dongyaoxiu6244 2016-04-28 17:59
    关注

    Ignoring the memory issues, it looks like you are just trying to change the associative array indexes from the column name to something else. Try doing it in the SELECT:

    SELECT `id` AS label, `sum` AS y FROM `somewhere` . . .
    

    Then your original $users = pg_fetch_all($result); should contain what you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化