【发布时间】:2014-09-25 03:13:35
【问题描述】:
我有一个多维数组
array(
"Airportbus"=>array(
"NO"=>array(
"from"=>"Barcelona",
"to"=>"Gerona"
)
),
"flight"=>array(
"SK455"=>array(
"from"=>"Gerona",
"to"=>"Stockholm",
"seat"=>"3A"
),
"SK22"=>array(
"from"=>"Stockholm",
"to"=>"New york",
"gate"=>"Gate 22",
"description"=>"Baggage wiil be transfered from your last leg",
"seat"=>"7B"
)
),
"train"=>array(
"78A"=>array(
"from"=>"Madrid",
"to"=>"Barcelona",
"seat"=>"45B"
)
)
);
我想打印这样的结果。
1. Take train 78A from Madrid to Barcelona. Sit in seat 45B.
2. Take the airport bus from Barcelona to Gerona Airport. No seat assignment.
3. From Gerona Airport, take flight SK455 to Stockholm. Gate 45B, seat 3A. Baggage drop at ticket counter 344.
4. From Stockholm, take flight SK22 to New York JFK. Gate 22, seat 7B. Baggage will we automatically transferred from your last leg.
这里的问题是
1.一些数组有更多的元素,不同的键"gate","description"。
2.一些额外的文字被打印在结果中,“Sit in”,“Take”。
我尝试用
打印结果$message = "";
if(issset($result['key']))
{
$message += " some text ".$result['key']. " some text ",
}
if(issset($result['key2']))
{
$message += " some text ".$result['key2']. " some text ",
}
if(issset($result['key2']))
{
$message += " some text ".$result['key2']. " some text ",
}
我认为 if 效率低下,因为每次添加新数组键时我都必须添加更多代码。
在这种情况下有没有更好的办法,请帮忙。在此先感谢:)
【问题讨论】:
-
至少从一个循环开始。您的数组与您的输出不匹配。你怎么知道顺序是train-bus-flight,数组有bus-flight-train
-
@Dagon 你能给我一个简单的例子吗:(
-
@Dagon , mm 没有订单 :(
-
那你怎么知道什么时候会发生什么?我先坐公交还是火车?
-
@Dagon 有一个算法可以做到这一点;),我唯一的问题是打印结果。我已经实现了算法
标签: php arrays string algorithm if-statement