简体   繁体   中英

Compare multiple values in two arrays PHP

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:

//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:

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 ?

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM