优化php中许多循环的建议?

时间:2015-11-01 07:34:53

标签: php

我一直在研究这个代码,它在这里和那里拼凑起来,因为它来了并且工作,结果非常好!

我只是需要一些建议,我应该做些什么来减少循环量,或者是否有任何你不应该看到的循环?

对以下代码的任何建议表示赞赏。

if (isset($_POST['refresh-history'])):
  $order_id = $_POST['id'];
  $order = $database->get_results('SELECT * FROM `orders` WHERE `order_id`='.$order_id);
  $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
  foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
  endforeach;
  $api->setRegion($region);
  $matchlistapi = $api->matchlist();
  $matchapi = $api->match();
  $matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5", "SEASON2015", null, null, null, $date_created, $date_completed);
  if ($matchlist->totalGames !== 0):
    foreach ($matchlist as $key):
      $gameIds[] = $key->matchId;
    endforeach;
    $arr_matches = object2array($matches);
    foreach ($arr_matches as $id) {
      $dbMatches[] = (int)$id->match_id;
    }
    $new_array = array_diff($gameIds, $dbMatches);

    foreach ($new_array as $matchId):
      $games[] = $matchapi->match($matchId, false);
    endforeach;
    foreach ($games as $game):
      // Store Games in DB;
    endforeach;
    $_SESSION['api_success'] = "Success: Games Synced to Order.";
  else:
    $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
  endif;
endif;

要清楚我不需要回答!只是朝着正确的方向推进,我可以从那里走。 :)

4 个答案:

答案 0 :(得分:9)

<强>解释

您可以替换以下代码

foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
endforeach;

//get the last order.
$order = end($orders);

//set the information.
$loluser = ($order === false) ? '' : $order->loluser;
$region = ($order === false) ? '' : $order->region;
$date_created = ($order === false) ? '' : $order->date_created;
$date_completed = ($order === false) ? '' : $order->date_completed;

解决了两个问题。首先,你的foreach运行所有订单并将每个订单的属性写入变量。最后,只会在变量上设置最后一个订单的属性。第二件事是,如果没有订单可用,则不在以下脚本上设置变量。

在同步部分,我可以删除一些不需要的变量,因为它们只为下一个循环存储一个数组。但是如果你以前没有检查过函数的结果,你可以在循环中使用函数本身的结果。

一个例子。您可以替换以下代码

$new_array = array_diff($gameIds, $dbMatches);

foreach ($new_array as $matchId):
    $games[] = $matchapi->match($matchId, false);
endforeach;

可以替换为

foreach (array_diff($gameIds, $dbMatches) as $matchId):
    $games[] = $matchapi->match($matchId, false);
endforeach;

结果

在这里,您可以通过一些优化找到脚本的完整代码:

<?php
if (isset($_POST['refresh-history'])) {
    $order_id = $_POST['id'];
    $orders = $database->get_results('SELECT * FROM `orders` WHERE `order_id` = '.$order_id);
    $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id` = '.$order_id);

    //get the last order.
    $order = end($orders);

    //set the information.
    $loluser = ($order === false) ? '' : $order->loluser;
    $region = ($order === false) ? '' : $order->region;
    $date_created = ($order === false) ? '' : $order->date_created;
    $date_completed = ($order === false) ? '' : $order->date_completed;

    $api->setRegion($region);
    $matchlistapi = $api->matchlist();
    $matchapi = $api->match();
    $matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5", "SEASON2015", null, null, null, $date_created, $date_completed);

    //check if a game is available.
    if ($matchlist->totalGames > 0) {

        //initialize the vars.
        $matchIds = array();
        $matchIdsDB = array();

        //collect all match ids from api.
        foreach ($matchlist as $match) {
            $matchIds[] = (int) $match->matchId;
        }

        //collect all match ids from database.
        foreach (object2array($matches) as $match) {
            $matchIdsDB[] = (int) $match->match_id;
        }

        //run through all missing matches.
        foreach (array_diff($matchIds, $matchIdsDB) as $match) {
             $game = $matchapi->match($match, false);

             //store game in database or create a big query to create all in one.
        }

        $_SESSION['api_success'] = "Success: Games Synced to Order.";
    } else {
        $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
    }
}

希望它有所帮助!

答案 1 :(得分:3)

此代码执行以下操作:

  • 获取与当前订单对应的数据库中的信息:

    'SELECT * FROM `orders` WHERE `order_id` = '.$order_id);
    

问题在于order_id应该是您的主键,因此不需要循环。

  • 获取与当前订单匹配的匹配列表

      $matchlistapi->matchlist(...)
    
  • 获取与数据库中当前订单匹配的匹配列表

  • 将游戏存储在数据库中但尚未存在

一切都没用。重要的是以独特的方式存储比赛。

只需在matches

上定义多列唯一键即可
ALTER TABLE matches ADD UNIQUE (order_id, ..., ...)

并在插入时使用INSERT IGNORE INTO matches

因此匹配表中已经存在的元素不会被覆盖。

您的代码类似于:

$result = $database->get_results('SELECT loluser, region, date_created, date_completed
             FROM `orders` WHERE `order_id` = '.$order_id);

list($loluser, $region, $date_created, $date_completed)=$result[0];

$matchlistapi = $api->matchlist();
$matchlist = $matchlistapi->matchlist($loluser, "RANKED_SOLO_5x5",
            "SEASON2015", null, null, null, $date_created, $date_completed);

$matchapi = $api->match();
foreach ($matchlist as $m) {
   $match=$matchapi->match($m->match_id, false);
   $stmt=$database->prepare("INSERT IGNORE INTO matches VALUES(?, ?, ?)", ...);
   $database->execute($stmt);
}

注意:当然,您必须在第一次请求中关注潜在的SQL注入

答案 2 :(得分:2)

 foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
  endforeach;

这个循环总是返回数组的最后一个值,那么在这里使用循环有什么意义呢?

对于其他循环,现在的目的是从数组元素中获取值?您可以以通用方式包装循环,并返回新数组。 WordPress有一个你可以重用的功能 https://codex.wordpress.org/Function_Reference/wp_list_pluck

答案 3 :(得分:0)

您不需要减少循环量或if else语句。使代码更具可读性的关键是将它们分组为逻辑组。考虑这样的事情。

function getMatchDetail($orderId) {
    $matchDetail = array();
    $order_id = $_POST['id'];
    $order = $database->get_results('SELECT * FROM `orders` WHERE `order_id`='.$order_id);
    $matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
    // I do not know what do get_results will return. So I am going to use my best guess
    $matchDetail[$match]["loluser"] = $order["loluser"];
    $matchDetail[$match]["region"] = $order["region"];
    $matchDetail[$match]["date_created"] = $order["date_created"];
    $matchDetail[$match]["date_completed"] = $order["date_completed"];
    return $matchDetail;
}

function getMatchListFromAPI($matchDetail) {
    // Again, just giving you a general direction;
    $api = new API();
    $api->setRegion($matchDetail["region"]);
    $matchList = $api->getList($matchDetail["loluser"], $matchDetail["date_created"], $matchDetail["date_completed"]);
    return $matchList;
}

function doStuffWithMatchList($matchList) {
    foreach ($matchlist as $key):
        $gameIds[] = $key->matchId;
    endforeach;
    $arr_matches = object2array($matches);
    foreach ($arr_matches as $id) {
        $dbMatches[] = (int)$id->match_id;
    }
    $new_array = array_diff($gameIds, $dbMatches);

    foreach ($new_array as $matchId):
        $games[] = $matchapi->match($matchId, false);
    endforeach;
    foreach ($games as $game):
        // Store Games in DB;
    endforeach;
}

if (isset($_POST['refresh-history'])):
    $matchDetail = getMatchDetail($_POST["id"]);
    $matchList = getMatchListFromAPI($matchDetail);
    if ($matchlist["totalGames"] !== 0):
        doStuffWithMatchList($matchList);
        $_SESSION['api_success'] = "Success: Games Synced to Order.";
    else:
        $_SESSION['error_msg'] = "Error 23: Unable to Find Games.";
    endif;
endif;

现在很清楚你的程序做了什么。

同时评论您的代码将有所帮助