【问题标题】:foreach code not working for filtering API [closed]foreach 代码不适用于过滤 API [关闭]
【发布时间】:2018-04-24 19:44:42
【问题描述】:

按城市和酒店搜索我的数据库中有哪些城市和酒店。我的代码不起作用。你能检查我的代码。可能是代码中的一些错误。

$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);结果:

array (size=0)
  empty

【问题讨论】:

标签: php sql wordpress api


【解决方案1】:

试试

if ($storedCountryName  == $apiResult['address']['city'] && $storedHotelName == $apiResult['property_name']) 
{
   $finalArray[] = $apiResult;

}

=== 表示您也在检查数据类型。您的数据库字段数据类型和 json 响应字符串数据类型可能不匹配。因此,从条件中删除一个 = 并仅比较值。

【讨论】:

  • 我可以知道我为什么得到 -1 吗?我不认为我的回答是错误的。
  • @downvoter 留下解释 :)
【解决方案2】:

试试这个并告诉我。

我将这一行 if ($dbResults->num_rows) { 替换为 if ($wpdb->num_rows>0) {

它应该可以工作。

    $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);

【讨论】:

  • 我将这一行 if ($dbResults->num_rows) { 替换为 if ($wpdb->num_rows>0) {
  • 它工作但只显示一个酒店信息!在 api url 中有两家酒店与我的数据库匹配
  • @rayan005,答案已更新。现在检查。
  • 还是一样的问题
  • 答案再次更新。你必须检查 if 条件。打印这些值并检查$storedCountryName && $storedHotelName
猜你喜欢
  • 1970-01-01
  • 2016-05-20
  • 2017-12-31
  • 1970-01-01
  • 2012-03-10
  • 2016-03-16
  • 2020-12-24
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多