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

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.

图片转代码服务由CSDN问答提供 功能建议

我有以下代码:

  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); \  n 24 / * 
 25 echo“&lt; pre&gt;”; 
 26 var_dump($ temp); 
 27 echo“&lt; / pre&gt;”; 
 28 * / 
 29 array_push($ chart_data,$  temp); 
 30} 
 31回显计数($ chart_data); 
   
 
 

当我运行我的代码时,收到以下错误消息:

  [Thu Apr 28 17:23:39.278844 2016] [:error] [pid 24321] [client 10.63.8.104:58362] PHP致命错误:允许的内存大小为134217728字节耗尽( 尝试在第21行的/var/www/localhost/htdocs/audit/index.php中分配20480字节)
   
 
 

这是我得到的输出示例 来自print_r():

 数组([label] =&g 吨;  12 [y] =&gt;  0.0021)
   
 
 

我无法看到第21行如何成为问题。 也许我需要明确地将temp重置为空数组? 当然也不确定,但欢迎提出任何建议。 谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题