比较两个数组PHP中的多个值

时间:2022-12-28 12:06:14

I have two MySQL Databases, and would like to compare the data using PHP variables. I connect to the databases and assign the variables using PDO:

我有两个MySQL数据库,并希望使用PHP变量比较数据。我连接到数据库并使用PDO分配变量:

//Database 1
include_once('client-config.php');
try {
    $conn = new PDO(DB_HOST, DB_USER, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => TRUE));
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$DB_Name = "pencuy204";
$login = $_SESSION['SESS_login'];
$qry = "SELECT `BetType`, `RiskAmount`, `WinAmount`, `BetDate`, `GameDate`, `BetRotation`, `TeamParticipant`, `MoneyLine`, `Spread`, `OverUnder`
        FROM `{$login}_bet`"; 
$result = $conn->query($qry);

// If the SQL query is succesfully performed ($result not false)
if ($result !== false) {
// Parse the result set, and adds each row and colums in HTML table
    foreach ($result as $row) {
        $BetType[] = $row['BetType'];
        $BetRiskAmount[] = $row['RiskAmount'];
        $BetWinAmount[] = $row['WinAmount'];
        $BetGameDate[] = strtotime($row['GameDate']);
        $BetTeamParticipant[] = $row['TeamParticipant'];
        $BetMoneyLine[] = $row['MoneyLine'];
        $BetSpread[] = $row['Spread'];
        $BetOverUnder[] = $row['OverUnder'];
    }
}

//Database 2
try {
    require_once('bet-config.php'); 
    $conn1 = new PDO(B_DB_HOST, B_DB_USER, B_DB_PASSWORD);
    $conn1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}   

date_default_timezone_set('CST');
$today = date("Y-m-d"); 
$qry = "SELECT `AwayTeam`, `AwayScore`, `HomeTeam`, `HomeScore`, `FeedDate` FROM games";
$checkit = $conn1->query($qry);

if ($checkit !== false) {
    foreach($checkit as $row1) {
        $AwayTeam[] = $row1['AwayTeam'];
        $HomeTeam[] = $row1['HomeTeam'];
        $AwayScoreData[] = $row1['AwayScore'];
        $HomeScoreData[] = $row1['HomeScore'];
        $FeedDate[] = strtotime($row1['FeedDate']);
    }
}

What I would like to do is run through each value in certain PHP arrays in Database 1, comparing them every value in certain arrays in Database 2. Here is an example for loop that I am working on:

我想要做的是遍历数据库1中某些PHP数组中的每个值,将它们与数据库2中某些数组中的每个值进行比较。以下是我正在处理的循环示例:

for ($i = 0; $i <= $count; $i++) { 
    foreach ($BetGameDate as $b) {
        if (($b == $FeedDate[$i])) {
            foreach ($BetTeamParticipant as $team) {
                if (($team == $AwayTeam[$i])) {
                    foreach ($BetType as $type) {
                        if (($type == "Money Line")) {
                            if ($AwayScoreData[$i] < $HomeScoreData[$i]) {
                                $BetV[] = "-" . $BetRiskAmount[$i];
                                $BetC[] = intval('$BetV');
                            }

                            if ($AwayScoreData[$i] > $HomeScoreData[$i]) {
                                $BetV[] = "+" . $BetWinAmount[$i];
                                $BetC[] = intval('$BetV');
                            }

                            if ($AwayScoreData[$i] == $HomeScoreData[$i]) {
                                $BetV[] = 0;
                                $BetC[] = intval('$BetV');
                            }
                        }
                    }
                }
            }
        }
    }
}

In this particular example, if $GameBetDate is equal to $FeedDate, the bet team name is equal to the away team name, and the bet type is equal to a certain string, then compute the bet based on the risk amount or win amount for that specific bet(row) in database 1. I feel like my use of foreach is correct, but how can I properly use an iterated for loop to cycle through all of the values in database 2 against the specific values in database 1, and if the criteria matches, use values from database 1 to calculate $BetC and BetV?

在此特定示例中,如果$ GameBetDate等于$ FeedDate,则投注团队名称等于客队名称,并且投注类型等于某个字符串,然后根据风险金额或赢取金额计算投注金额。数据库1中的特定下注(行)。我觉得我对foreach的使用是正确的,但我怎样才能正确使用迭代for循环来循环数据库2中的所有值与数据库1中的特定值,如果标准匹配,使用数据库1中的值来计算$ BetC和BetV?

1 个解决方案

#1


0  

I think you can use a little refactoring in your code. To compare values you can use array_diff method. You select the values from the first table, (PDO can return array) Select second values and compare...

我想你可以在你的代码中使用一些重构。要比较值,可以使用array_diff方法。您从第一个表中选择值,(PDO可以返回数组)选择第二个值并比较...

http://php.net/manual/en/function.array-diff.php

http://php.net/manual/en/function.array-diff.php

#1


0  

I think you can use a little refactoring in your code. To compare values you can use array_diff method. You select the values from the first table, (PDO can return array) Select second values and compare...

我想你可以在你的代码中使用一些重构。要比较值,可以使用array_diff方法。您从第一个表中选择值,(PDO可以返回数组)选择第二个值并比较...

http://php.net/manual/en/function.array-diff.php

http://php.net/manual/en/function.array-diff.php