我需要一些帮助来循环遍历一组对象,这些对象表示从国家 a 到 b 的航类优惠,每条路线可能包含多个优惠。我想捕获每条航线的最低航类报价......我只是不知道如何在循环内做到这一点。这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;
$off 转储返回以下内容
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 .....
有什么想法吗?
非常感谢!
最佳答案
我想你可以跳过最后一个循环。
<?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'].
}
关于PHP循环并获得最低价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129717/
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我是Ruby的新手,有些闭包逻辑让我感到困惑。考虑这段代码:array=[]foriin(1..5)array[5,5,5,5,5]这对我来说很有意义,因为i被绑定(bind)在循环之外,所以每次循环都会捕获相同的变量。使用每个block可以解决这个问题对我来说也很有意义:array=[](1..5).each{|i|array[1,2,3,4,5]...因为现在每次通过时都单独声明i。但现在我迷路了:为什么我不能通过引入一个中间变量来修复它?array=[]foriin1..5j=iarray[5,5,5,5,5]因为j每次循环都是新的,我认为每次循环都会捕获不同的变量。例如,这绝对
当您在Ruby脚本中使用系统调用时,您可以像这样获得该命令的输出:output=`ls`putsoutput这就是thisquestion是关于。但是有没有办法显示系统调用的连续输出?例如,如果您运行此安全复制命令,以通过SSH从服务器获取文件:scpuser@someserver:remoteFile/some/local/folder/...它显示随着下载进度的连续输出。但是这个:output=`scpuser@someserver:remoteFile/some/local/folder/`putsoutput...不捕获该输出。如何从我的Ruby脚本中显示正在进行的下载进度?
假设我有一个没有特定顺序的随机数数组。假设这些是参加马拉松比赛的人的ID#,他们按照完成的顺序添加到数组中,例如:race1=[8,102,67,58,91,16,27]race2=[51,31,7,15,99,58,22]这是一个简化且有些做作的示例,但我认为它传达了基本思想。现在有几个问题:首先,我如何获得特定条目之前和之后的ID?假设我正在查看运行者58,我想知道谁在他之前和之后完成了比赛。race1,runner58:previousfinisher=67,nextfinisher=91race2,runner58:previousfinisher=99,nextfinishe
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就
在EloquentRuby(第21页,第一版,第六次打印)一书中,作者(RussOlsen)提倡使用each方法而不是for循环,这与我在其他地方读到的所有内容一致。但是作者还继续说,这样做的一个原因是for循环实际上调用了each方法,所以为什么不直接删掉中间人并使用each?所以我想知道这实际上是如何工作的。为了调查,我确实在github上的Ruby存储库上进行了搜索,但发现很难确定我在哪里/如何看到它的实际效果。重述问题:我如何证明Rubyfor循环实际上是使用each方法实现的? 最佳答案 您可以通过编写一个实现每个的类来展
我想从0到2循环@a:0,1,2,0,1,2。defset_aif@a==2@a=0else@a=@a+1endend也许有更好的方法? 最佳答案 (0..2).cycle(3){|x|putsx}#=>0,1,2,0,1,2,0,1,2item=[0,1,2].cycle.eachitem.next#=>0item.next#=>1item.next#=>2item.next#=>0... 关于ruby-循环遍历数组的元素,我们在StackOverflow上找到一个类似的问题: