doudou8893 2013-01-30 01:05
浏览 232
已采纳

致命错误:允许的内存大小为134217728字节耗尽(尝试分配36个字节)

i have made a function will return N-th digit of sequence number, example :

1234567891011121314151617

  • The 5-th digit is 5
  • The 20-th digit is 1

My function is like this

 <?php
   function getLength($number) {
   $length = 0;
   if ($number == 0){
      $length = 1;
   } else {
      $length = (int) log10($number)+1;
   }
     return $length;
  }
 ?>

<?php
 function getDigitNumber($digit){
   $number = 10000000000;
   $data = array();
   for($i=1;$i<=$number;$i++){  
   if(getLength($i) > 1){
     $array = str_split($i,1);
    for($n=0;$n<=count($array)-1;$n++){
        array_push($data,$array[$n]);
    }   
}else{
    $data[$i] = $i;
}
  } 
return $data[$digit];
}
?>

When i executed that i've a problem like this error message "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)". Maybe because i assign a large number in var $number

How can i fix that..or how can i make function that can handle these large number

thanks..

  • 写回答

2条回答 默认 最新

  • duancan1950 2013-01-30 01:08
    关注

    i have made a function will return N-th digit of sequence number, example :

    1234567891011121314151617

    The 5-th digit is 5
    The 20-th digit is 1
    

    Why not:

    $number[5];
    $number[20];
    

    The number has to be a string anyways, so just access the character with array syntax.

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

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗