dongshuo5101 2015-03-24 12:46
浏览 61
已采纳

内存在嵌套循环中使用数组耗尽

This is the error I'm getting.

Fatal Error: Allowed memory size exhausted.

I'm fetching arrays containing a date from and a date till. I'm trying to get all the dates in between and add them to a new array. Apparently nested loops and multi-arrays are exhausting.

I need a less exhausting way of getting all the dates.

This is my code:

$query = "SELECT *
          FROM reservate
          ORDER BY from";
$result = $connect->query($query);

$reservations = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $reservations[] = $row;
    }
}

$connect->close();

//AVAILABILITY CHECK
$nonAvailable = array();
foreach($reservations as $reservation){
    $from = $reservation['from'];
    $till = $reservation['till'];

    while($from <= $till){
        $nonAvailable[] = $from;
        $from = date('Y-m-d', strtotime("+1 days"));
    }
}
  • 写回答

2条回答 默认 最新

  • dstbp22002 2015-03-24 12:52
    关注

    It looks like you made an infinite loop1.

    // if $till is in the future, this is an infinite loop
    while($from <= $till){
        // appending the same value of $from on each iteration
        $nonAvailable[] = $from;
        // as $from never changes value
        $from = date('Y-m-d', strtotime("+1 days"));
    }
    

    Add 1 day to the current value of $from

    while ($from <= $till){
         $nonAvailable[] = $from;
         // add 1 day to $from
         $from = date('Y-m-d', strtotime($from, "+1 days"));
    }
    

    There are still a few improvements that can be made:

    • In the following example the entire table is not copied to the reservation array and we are only retrieving the columns we need from the table.
    • Comparing integers instead of strings.
    • Adding 1 day to $from manually rather then relying on strtotime to do it.
    $query = "SELECT from, till
              FROM reservate
              ORDER BY from";
    $result = $connect->query($query);
    
    $nonAvailable = array();
    
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $from = strtotime($row['from']);
            $till = strtotime($row['till']);
    
            while ($from <= $till) {
                $nonAvailable[] = date('Y-m-d', $from);
                $from += 60*60*24;
            }
        }
    }
    
    $connect->close();
    

    It is likely that this algorithm can be made much more efficient. For example do you need an exhaustive list of each day something is reserved? If not then just the start date and end date should suffice.


    1 Given enough time and memory the loop would exit as strtotime("+1 days") would eventually return a value greater than $till.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测