【问题标题】:PHP loop and get lowest pricePHP循环并获得最低价格
【发布时间】:2015-09-16 17:52:45
【问题描述】:

我需要一些帮助来循环一组代表从国家ab 的航班报价的对象,并且每条路线可能包含多个报价。我想为每条路线获取最低的航班报价......我只是不知道如何在循环内做到这一点。这个Answer on SO我试过的作品会给我最低的价格,但我怎样才能得到这个价格的整个阵列?我知道这听起来很愚蠢,但我卡住了。

 $results = array();
 $offers = R::getAll( "SELECT * FROM fares WHERE available = 1 LIMIT 4");

$offers返回的数据集样本发布在Fiddle

然后我做了以下事情:

 foreach($offers as $key=>$val){
            $results ['city'] = $val["name"];
            $results ['country'] = $val["parent_name"];
            $off = json_decode($val["Flights"]);
            var_dump($off);

            // here I need to loop through $off & grab the lowest
        }

   return $results;

$offdump 返回以下内容

object(stdClass)#60 (1) {
  ["BEY"]=>
  array(4) {
    [0]=>
    object(stdClass)#61 (6) {
      ["price"]=>
      int(490)
      ["airline"]=>
      string(2) "ME"
      ["flight_number"]=>
      int(276)
      ["departure_at"]=>
      string(20) "2015-07-11T12:20:00Z"
      ["return_at"]=>
      string(20) "2015-07-18T08:20:00Z"
      ["expires_at"]=>
      string(20) "2015-06-30T14:31:43Z"
    }
    [1]=>
    object(stdClass)#62 (6) {
      ["price"]=>
      int(639)
      ["airline"]=>
      string(2) "FZ"
      ["flight_number"]=>
      int(716)
      ["departure_at"]=>
      string(20) "2015-07-05T15:50:00Z"
      ["return_at"]=>
      string(20) "2015-07-12T04:30:00Z"
      ["expires_at"]=>
      string(20) "2015-07-01T08:11:49Z"
    }
    [2]=>
    object(stdClass)#63 (6) {
      ["price"]=>
      int(472)
      ["airline"]=>
      string(2) "EY"
      ["flight_number"]=>
      int(299)
      ["departure_at"]=>
      string(20) "2015-07-18T03:10:00Z"
      ["return_at"]=>
      string(20) "2015-07-28T04:30:00Z"
      ["expires_at"]=>
      string(20) "2015-06-30T12:20:30Z"
    }
    [3]=>
    object(stdClass)#64 (6) {
      ["price"]=>
      int(2045)
      ["airline"]=>
      string(2) "SU"
      ["flight_number"]=>
      int(1861)
      ["departure_at"]=>
      string(20) "2015-07-11T14:50:00Z"
      ["return_at"]=>
      string(20) "2015-07-18T05:05:00Z"
      ["expires_at"]=>
      string(20) "2015-06-30T14:31:43Z"
    }
  }
} 

object(stdClass)#65 (1) {
  ["BEY"]=>
  object(stdClass)#66 (3) {
    ["1"]=>
    object(stdClass)#67 (6) {
      ["price"]=>
      int(1903)
      ["airline"]=>
      string(2) "EY"
      ["flight_number"]=>
      int(461)
      ["departure_at"]=>
      string(20) "2015-07-17T22:40:00Z"
      ["return_at"]=>
      string(20) "2015-07-31T04:30:00Z"
      ["expires_at"]=>
      string(20) "2015-07-01T10:46:41Z"
    }
    ["2"]=>
    object(stdClass)#68 (6) {
      ["price"]=>
      int(1535)
      ["airline"]=>
      string(2) "QF"
      ["flight_number"]=>
      int(341)
      ["departure_at"]=>
      string(20) "2015-07-17T11:00:00Z"
      ["return_at"]=>
      string(20) "2015-07-31T22:55:00Z"
      ["expires_at"]=>
      string(20) "2015-07-01T10:46:41Z"
    }
    ["3"]=>
    object(stdClass)#69 (6) {
      ["price"]=>
      int(2321)
      ["airline"]=>
      string(2) "AY"
      ["flight_number"]=>
      int(5014)
      ["departure_at"]=>
      string(20) "2015-07-17T11:45:00Z"
      ["return_at"]=>
      string(20) "2015-07-31T22:55:00Z"
      ["expires_at"]=>
      string(20) "2015-07-01T10:46:41Z"
    }
  }
}
ect .....

有什么想法吗?

非常感谢!

【问题讨论】:

  • 根据 $off 的结果,它不是单个对象。它包含多个对象。那么您想要的确切响应是什么?
  • @Awena,您能否分享您正在使用的表结构,请以纯数组而不是对象的形式显示数据。
  • 获取每条航线的最低航班报价对于 SQL 来说似乎是一项完美的工作。
  • @Purushottamzende 给我一秒钟。我不认为 SQL 可以查询 JSON 数据,可以吗?你看小提琴了吗?

标签: php arrays loops foreach


【解决方案1】:

我认为你可以跳过最后一个循环。

<?php
foreach ($offers as $key => $val) {
    $results['city'] = $val["name"];
    $results['country'] = $val["parent_name"];
    $off = json_decode($val["Flights"], true);

    $lowest = PHP_INT_MAX;
    $lowest_item = null;
    foreach ($off['BEY'] as $item) {
        $lowest = min($item['price'], $lowest);
        if ($item['price'] == $lowest) {
            $lowest_item = $item;
        }
    }
    // Now $lowest is the lowest price and 
    // you can do something with $lowest_item, 
    // like $lowest_item['flight_number'].
}

【讨论】:

    【解决方案2】:

    试试这个:

    foreach($offers as $key=>$val){
        $results ['city'] = $val["name"];
        $results ['country'] = $val["parent_name"];
    
        $off = json_decode($val["Flights"], true); //note the true
    
        $lowest = PHP_INT_MAX;
        foreach ($off['BEY'] as $item)
            $lowest = min($item['price'], $lowest);
    
        //now $lowest is the lowest price
    
        $lowest_item = null;
        foreach ($off['BEY'] as $item)
            if ($item['price'] == $lowest) {
                $lowest_item = $item;
                break;
            }
    
        //now you can do something with $lowest_item, like $lowest_item['flight_number']
    }
    

    【讨论】:

    • 谢谢先生!终于有人在发帖前读了一个问题!我很感激戴夫。干杯
    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 2018-10-12
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2021-01-07
    • 2012-07-11
    • 2021-10-19
    相关资源
    最近更新 更多