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

标签 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;

明确地说,我不需要答案!只需朝正确的方向推一下,我就可以从那里开始。 :)

最佳答案

解释

你可以替换下面的代码

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.";
    }
}

希望对您有所帮助!

关于php - 优化 php 中许多循环的建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460134/

相关文章:

php - 将值从一个 div 拖放到另一个 div 并在拖放后进行 mysql 查询

javascript - 使用 AJAX 和 PHP 更改组合框选择

java - preg_match() : Text range out of order on PHP Regex

php - 获取从资源传递给 fopen 的模式?

php - 德鲁巴 8 : How do i fix the irritating error message "Unable to install advertisement, already exist in active configuration"?

php - 配置文件的最佳文件格式是什么

php - sSMTP 不再有效 - 'Invalid response: 501 5.5.4 HELO/EHLO argument MYEMAILADDRESS@gmail.com invalid, closing connection.'

PHP/MySQL计算具有相同外键的项目之间的差异

php - 从 php 执行的 python 脚本出现错误

php - 如何使用 curl_setopt() 将提供的带有内联参数的单行 cURL 代码转换为格式