普通网友 2012-01-13 06:00
浏览 48
已采纳

php代码改进重构

Hi staskoverflow community.

How can i improve next structure of PHP 5 code. A list of models $months (more than 12) $month->days (more than 31)

$day->clients a list of clients $day->someActions a list of actions for $clients

i need to invoke every $action for every $client. So my construction is:

<?php
foreach($monthes as $month) {
    foreach($month->days as $day) {
        foreach($day->clients as $client) {
            foreach($day->actions as $action) {
                $action->run($client);
            }
        }
    }
} ?>

So there are four foreach loops, can i improve this code ?

  • 写回答

1条回答 默认 最新

  • duanluwei9374 2012-01-13 09:11
    关注

    Why do you want to move out of nested loops? the code you have is very clear to understand and if your objective is to optimize the performance of this method, then the approach I would go with would be:

    1. I will first try to reduce the number of iterations. you can do this by asking questions to yourself like how many months data do I actually want to display on the go?
    2. Similar questions can be asked about days and clients
    3. do you have a break condition? What i mean by this is,

      $result = $action->run($client); if("break_condition" == $result){ break; }

    this should considerably reduce the number of iterations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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