PHP loop and get lowest price

Awena

I need please some help with the looping through a set of objects that represents flight offers from country a to b and each route may contain multiple offers. I want to grab the lowest flight offer for each route... I just don't know how to do it inside the loop. This Answer on SO I tried works will giving me the lowest price but how can I get the whole array of this price? I know it sounds stupid but im stuck.

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

$offers returned data set sample posted here on Fiddle

then I did the following :

 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;

$off dump returns the following

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 .....

Any idea ?

Thanks alot!

Dave Chen

Try this:

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']
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

c++ loop for lowest common multiple

来自分类Dev

wc_price($ price)未按FILTER“ woocommerce_product_get_regular_price”显示折扣

来自分类Dev

get_buy_price()的API Coinbase ValueError

来自分类Dev

编辑Woocommerce get_price_html()格式

来自分类Dev

未捕获的TypeError:.get $ price不是函数

来自分类Dev

get_buy_price()的API Coinbase ValueError

来自分类Dev

Woocommerce“woocommerce_product_get_price”问题

来自分类Dev

How to get the closest number from a List<Price> with LINQ?

来自分类Dev

没有路线与[GET]“ /price.txt”匹配

来自分类Dev

PHP- Get each value of an associative array in a foreach loop, then apply a function and echo the result

来自分类Dev

PHP For Loop不会迭代

来自分类Dev

PHP For Loop不会迭代

来自分类Dev

loop through the files in a folder in php

来自分类Dev

PHP Loop JSON ajax成功

来自分类Dev

如何激活Loop Tab PHP

来自分类Dev

用于 php 到 Postgres FOR LOOP

来自分类Dev

Woocommerce钩子woocommerce_get_price如何在产品列表中使用?

来自分类Dev

这行发生了什么$ this-> config-> get('config_customer_price')

来自分类Dev

WooCommerce自定义模板price.php上的小数点分隔符问题

来自分类Dev

我想显示使用PHP的Gold Price India网站当前的GOLD RATE

来自分类Dev

.htaccess PHP $ _GET

来自分类Dev

PHP $ _GET ['action']

来自分类Dev

清理$ _GET数据-PHP

来自分类Dev

PHP $ _GET和HTML

来自分类Dev

$ _GET的PHP问题

来自分类Dev

PHP $ _GET .htaccess重写

来自分类Dev

重写GET变量php

来自分类Dev

PHP $ _POST,$ _ GET或$ _REQUEST

来自分类Dev

PHP多个$ _GET