【问题标题】:Comparison of API results and database results in phpphp中API结果与数据库结果对比
【发布时间】:2016-01-25 10:14:39
【问题描述】:

我正在使用即将到来的 API 的结果映射数据库结果,以便显示阵列中存在的最低价格的房间和其他独特的房间。 我创建了两个多维数组,一个用于我的数据库结果,另一个用于基于 API 的结果。这样我就可以比较值并显示在 API 结果和数据库结果中都存在的房间的最低价格以及在这两种方式中都是唯一的其他剩余值 以下是我创建的两个数组

 $arr1  =  [0] => Array
                (
                    [roomName] => Standard
                    [ratePlan] => CPAI
                    [roomRate] => 10000
                )
            [1] => Array
                (
                    [roomName] => Standard
                    [ratePlan] => MAP
                    [roomRate] => 11000
                )

        $arr2 = [0] => Array
                (
                    [roomName] => Standard
                    [ratePlan] => CP
                    [roomRate] => 9000
                )

            [1] => Array
                (
                    [roomName] => Standard
                    [ratePlan] => MAP
                    [roomRate] => 10800
                )  

我需要根据房间名称、房价计划和最低房价显示结果。为此,我需要比较以上两个数组的房间名称、房间计划和房价。 例如在第一个阵列中,有 MAP 计划的标准房间,价格为 11000,第二个阵列相同的房间,相同的计划,价格为 10800。所以我需要显示 10800 房间和其他剩余房间。 一件事我也必须展示其他剩余的结果。 我该如何解决这个问题。

【问题讨论】:

  • 我尝试使用 for each 来迭代两个数组,但多次给我一个值

标签: php arrays multidimensional-array


【解决方案1】:

检查下面的代码。

<?php

$arr1  =   array(
    array (
                'roomName' => 'Standard',
                'ratePlan' => 'CPAI',
                'roomRate' => 10000
            ),
      array
            (
                'roomName' => 'Standard',
                'ratePlan' => 'MAP',
                'roomRate' => 11000
            ));

$arr2  =   array(
    array (
                'roomName' => 'Standard',
                'ratePlan' => 'CPAI',
                'roomRate' => 10000
            ),
      array
            (
                'roomName' => 'New',
                'ratePlan' => 'MAP',
                'roomRate' => 10800
            ));

$data = array_merge($arr1,$arr2);



// Obtain a list of columns
foreach ($data as $key => $row) {
    $roomRate[$key]  = $row['roomRate'];
}

// Sort the data with roomRate Ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($roomRate, SORT_ASC,  $data);

为所有人更新代码,

 // Obtain a list of columns
foreach ($data as $key => $row) {
$roomName[$key]  = $row['roomName'];
$ratePlan[$key]  = $row['ratePlan'];
$roomRate[$key]  = $row['roomRate'];
}


// Add $data as the last parameter, to sort by the common key
array_multisort($roomName,SORT_ASC, $ratePlan, SORT_ASC, $roomRate, SORT_ASC,  $data);

【讨论】:

  • 我必须比较第一个房间名称,如果它相同,那么它的计划,然后是价格。如果最低,我必须显示
  • 它没有给出任何结果
  • 我想要这样的结果 [0] => 数组 ( [hotelId] => 51991, [roomId] => 6613, [roomName] => Standard, [ratePlan] => EP,[roomRate ] => 9000) [1] => Array([hotelId] => 51991, [roomId] => 6614, [roomName] => 标准,[ratePlan] => CPAI,[roomRate] => 10000 [2] = > Array([hotelId] => 51991,[roomId] => 10176, [roomName] => 标准,[ratePlan] => MAP, [roomRate] => 10800)
  • @varadmayee 输入是什么?
  • 请检查链接codepad.org/NtAppj6n 和输出,我希望这将满足您的需求,更改您的输入并测试该代码,无论如何,让我知道
猜你喜欢
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2011-12-10
  • 2017-08-30
  • 1970-01-01
相关资源
最近更新 更多