douqi1931 2017-05-29 09:49
浏览 39
已采纳

foreach代码不适用于过滤API [关闭]

Search by city & hotels which city and hotels have on my database. My code is not worked. Can you check my code. Maybe some mistake in the code.

$countryName =  $_GET['countrycode'];
$url        =  'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response  = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];

$finalArray = [];
$dbResults   = $wpdb->get_results(
    $wpdb->prepare("
        SELECT * 
          FROM hotels
         WHERE county = %s",
        $countryName
    ),
    ARRAY_A 
);


# If atleast 1 row is fecthed from
# database
if ($dbResults->num_rows) {
    foreach ($dbResults as $dbResult) {
        echo $storedCountryName = $dbResult["county"];
        echo $storedHotelName   = $dbResult["hotel"];
        foreach ($apiResults as $apiResult) {
            if ($storedCountryName  === $apiResult['address']['city']
                && $storedHotelName === $apiResult['property_name']) 
            {
                $finalArray[] = $apiResult;


            }

        }

    }
}
var_dump($finalArray);

var_dump($finalArray); Result:

array (size=0)
  empty
  • 写回答

2条回答 默认 最新

  • doudouji2016 2017-05-29 10:08
    关注

    Try this and let me know.

    I replace this line if ($dbResults->num_rows) { with if ($wpdb->num_rows>0) {.

    It should work.

        $countryName =  $_GET['countrycode'];
    $url        =  'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
    $response  = file_get_contents($url);
    $apiResults = json_decode($response, true);
    $apiResults = $apiResults["results"];
    
    $finalArray = [];
    
    $dbResults   = $wpdb->get_results(
        $wpdb->prepare("
            SELECT * 
              FROM hotels
             WHERE county = %s",
            $countryName
        ),
        ARRAY_A 
    );
    
    
    # If atleast 1 row is fecthed from
    # database
    if ($wpdb->num_rows>0) {
        foreach ($dbResults as $dbResult) {
            echo $storedCountryName = $dbResult["county"];
            echo $storedHotelName   = $dbResult["hotel"];
            foreach ($apiResults as $apiResult) {
                $ResultArray= array();
                if ($storedCountryName  == $apiResult['address']['city']
                    && $storedHotelName == $apiResult['property_name']) 
                {
                    $ResultArray = $apiResult;
                    $finalArray[] = $ResultArray;
    
                }
    
            }
    
        }
    }
    var_dump($finalArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算