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

Ele*_*ant 4 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;
Run Code Online (Sandbox Code Playgroud)

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

Seb*_*sch 9

说明

您可以替换以下代码

foreach ($order as $o):
    $loluser = $o->loluser;
    $region = $o->region;
    $date_created = $o->date_created;
    $date_completed = $o->date_completed;
endforeach;
Run Code Online (Sandbox Code Playgroud)

//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;
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

可以替换为

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

结果

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

<?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.";
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!