【问题标题】:How can I call external array inside of foreach loop that is dealing with another array如何在处理另一个数组的 foreach 循环内调用外部数组
【发布时间】:2016-07-03 18:24:10
【问题描述】:

我有一个包含数据(站点和时间)的外部数组。 我想比较该数据是否已存在于我的数据库中,如果它不存在插入该特定站点,或者时间是否不等于数据库中的时间更新它。我只是不知道如何在循环数据库数据的 foreach 循环中调用外部数据,所以我暂时将插入和更新部分放在一边。

这是我的代码:

<?php
// all database parameters
require_once("config.php");

// main array usually contains up to around 40 arrays
$data_array = array(
                array("time"=>"2016-07-01 19:00:00", "name_station"=>"RC Bilogora", "type_station"=>"1", "longitude"=>"17.162", "latitude"=>"45.882", "temperature"=>"28.8"),
                array("time"=>"2016-07-01 19:00:00", "name_station"=>"Bjelovar", "type_station"=>"1", "longitude"=>"16.869", "latitude"=>"45.910", "temperature"=>"28.6"),
                array("time"=>"2016-07-01 19:00:00", "name_station"=>"Crikvenica", "type_station"=>"1",  "longitude"=>"14.689", "latitude"=>"45.173", "temperature"=>"28.0")
                );

// print $data_array
echo "Data from external array";
echo "<pre>";
print_r($data_array);
echo "</pre>";
echo "<hr>";

try {
        // connect to the database
        $dbh = new PDO('pgsql:host='.DB_HOST. ';port=' .DB_PORT. ';dbname=' .DB_NAME, DB_USER, DB_PASS);

        // start transaction
        $dbh->beginTransaction();

        // query
        $sql_select = "SELECT station, time FROM vwstationmeasurmentdva";
        $stmt_select = $dbh->prepare($sql_select);
        $stmt_select->execute();
        $result_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);

        // print results from sql for view stationmeasurment
        echo "Station and time from database captured with FETCH ASSOC";
        echo "<pre>";
        print_r($result_select);
        echo "</pre>";
        echo "<hr>";

        $number_of_rows = count($result_select);

        // FOREACH loop
        $result_vwstationmeasurment_select_array = array ();
        foreach($result_select as $row) {
            $station_name_db = $row['station'];
            $time_db = $row['time'];
            echo $station_name_db . "<br>";
            echo $time_name_db . "<br>";

            // PROBLEM: I DONT KNOW HOW TO CALL $station_name AND $time BEFORE THIS IF STATEMENT SO I CAN DO THE IF STATEMENT
            if ($station_name_db != $station_name OR empty($station_name_db)){
               echo "This station is not in a database or there are no data for stations in a database at all so insert that station";
            } elseif ($time_db != $time){
               echo "Time from external data is not equal to the time in a database so update time in a database";
            }

            $result_vwstationmeasurment_select_array[] =($row);
    };


        // save transaction
        $dbh->commit();

        // close database connection
        $dbh = null;

} catch (PDOException $e) {
        // cancel the transaciton if something went wrong and write msg about it
        $dbh->rollBack();
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
}
?>

打印 $data_array:

Data from external array
Array
(
    [0] => Array
        (
            [time] => 2016-07-01 19:00:00
            [name_station] => RC Bilogora
            [type_station] => 1
            [longitude] => 17.162
            [latitude] => 45.882
            [temperature] => 28.8
        )

    [1] => Array
        (
            [time] => 2016-07-01 19:00:00
            [name_station] => Bjelovar
            [type_station] => 1
            [longitude] => 16.869
            [latitude] => 45.910
            [temperature] => 28.6
        )

    [2] => Array
        (
            [time] => 2016-07-01 19:00:00
            [name_station] => Crikvenica
            [type_station] => 1
            [longitude] => 14.689
            [latitude] => 45.173
            [temperature] => 28.0
        )

)

打印 $result_select:

使用 FETCH ASSOC 捕获的数据库中的站点和时间

Array
(
    [0] => Array
        (
            [station] => RC Bilogora
            [time] => 2016-07-01 16:00:00
        )

    [1] => Array
        (
            [station] => Bjelovar
            [time] => 2016-07-01 16:00:00
        )

    [2] => Array
        (
            [station] => Dubrovnik
            [time] => 2016-07-01 16:00:00
        )

    [3] => Array
        (
            [station] => Gospić
            [time] => 2016-07-01 16:00:00
        )

    [4] => Array
        (
            [station] => RC Gorice (kod Nove Gradiške)
            [time] => 2016-07-01 16:00:00
        )

    [5] => Array
        (
            [station] => RC Gradište (kod Županje)
            [time] => 2016-07-01 16:00:00
        )

    [6] => Array
        (
            [station] => Hvar
            [time] => 2016-07-01 16:00:00
        )

    [7] => Array
        (
            [station] => Karlovac
            [time] => 2016-07-01 16:00:00
        )
...

FOREACH 循环:

RC Bilogora
2016-07-01 16:00:00
Bjelovar
2016-07-01 16:00:00
Dubrovnik
2016-07-01 16:00:00
Gospić
2016-07-01 16:00:00
RC Gorice (kod Nove Gradiške)
2016-07-01 16:00:00
RC Gradište (kod Županje)
2016-07-01 16:00:00
Hvar
2016-07-01 16:00:00
Karlovac
2016-07-01 16:00:00...

已编辑 (@PaulH)

我的方法根本行不通...你能通过代码给我一些建议吗...

try {
        // connect to the database
        $dbh = new PDO('pgsql:host='.DB_HOST. ';port=' .DB_PORT. ';dbname=' .DB_NAME, DB_USER, DB_PASS);

        // start transaction
        $dbh->beginTransaction();

        // EDITED
        foreach($data_array as $row){
                $station_external = $row['name_station'];
                $time_external = $row['time'];
                echo $station_external . "<br>";
                echo $time_external . "<br>";


                // query
                $sql_select = "SELECT station, time FROM vwstationmeasurmentdva";
                $stmt_select = $dbh->prepare($sql_select);
                $stmt_select->execute();
                $result_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);

                // print results from sql for view stationmeasurment
                echo "Station and time from database captured with FETCH ASSOC";
                //echo "<h2>Ovo su stanica i vrijeme dohvaceni iz baze podataka samo uz pomoc print_r nakon FETCH ASSOC</h2>";
                echo "<pre>";
                print_r($result_select);
                echo "</pre>";
        } // THE END OF EDIT

                // save transaction
                $dbh->commit();

                // close database connection
                $dbh = null;

} catch (PDOException $e) {
                // cancel the transaciton if something went wrong and write msg about it
                $dbh->rollBack();
                print "Error!: " . $e->getMessage() . "<br/>";
                die();
}

打印出来:

RC Bilogora
2016-07-01 19:00:00
Station and time from database captured with FETCH ASSOC
Array
(
    [0] => Array
        (
            [station] => RC Bilogora
            [time] => 2016-07-01 16:00:00
        )

    [1] => Array
        (
            [station] => Bjelovar
            [time] => 2016-07-01 16:00:00
        )
...
Bjelovar
2016-07-01 19:00:00
Station and time from database captured with FETCH ASSOC
Array
(
    [0] => Array
        (
            [station] => RC Bilogora
            [time] => 2016-07-01 16:00:00
        )

    [1] => Array
        (
            [station] => Bjelovar
            [time] => 2016-07-01 16:00:00
        )
...

【问题讨论】:

  • 我认为你应该反过来做:对于 $data_array 中的每个站点:查询数据库,比较结果并采取相应措施(即什么都不做,插入或更新数据库)。
  • 您想知道$station_name_db 是否存在于$data_array 中?
  • @精神反过来!
  • @PaulH 谢谢!因此,通过该语句,我可以检查数据库中是否存在来自外部阵列的特定时间的站和测量值。我现在如何基于此执行 IF 语句,以便我可以插入或更新数据?
  • @PaulH,你帮了我很多!多亏了你,我想我走对了!我稍后会更新我的问题作为答案!

标签: php arrays foreach


【解决方案1】:

答案基于 PaulH 的 cmets,我向他表示感谢。教别人的好方法,谢谢你的指导。

    foreach($data_array as $row){
            // station name from array
            $station_name = $row['name_station'];

            // query
            $sql_select = "SELECT station FROM vwstationmeasurmentdva WHERE station='$station_name'";
            $stmt_select = $dbh->prepare($sql_select);
            $stmt_select->execute();
            $result_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);

            $number_of_rows = count($result_select);
            if ($number_of_rows < 1){
                    echo " <br> Station doesn't exist in a database, we have to insert it. <br>";
            } else {
                    echo " <br> Station does exist in a database, so we will update the measurments. <br>";
           }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 2017-07-22
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多