【问题标题】:What is the best way to display the result of this array显示此数组结果的最佳方式是什么
【发布时间】: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


【解决方案1】:

我希望这个毛边草图能带你到某个地方

$out='';
foreach($resut as $k=>$v){
if($k=='Airportbus'){
//process bus
}

if($k=='Train'){
//process train
foreach($v as $kk=>$vv){

$trainid=$kk;
$from=$v[$kk]['from'];
$to=$v[$kk]['to'];
$seat=$v[$kk]['seat'];
$out .='you sit in.'$seat.' from '.$from.' to '.$to.' on train: '.$trainid;                               
}
}

} 

替代创建函数:

function train($data){
 foreach($data as $kk=>$vv){

    $trainid=$kk;
    $from=$v[$kk]['from'];
    $to=$v[$kk]['to'];
    $seat=$v[$kk]['seat'];
    $out .='you sit in.'$seat.' from '.$from.' to '.$to;                               
    }

return $out;

}

【讨论】:

  • 你还需要什么?
  • 你可能想为每个传输创建一个函数,解析数组并返回字符串,更简洁的方法
【解决方案2】:

这也可能满足您的需求(至少足够接近):

$transport_data = YOUR_DATA;

$count = 1;
foreach ($transport_data as $transport_type => $transports) {
    foreach($transports as $transport_id => $transport_details) {
        echo $count . "." . " Take " . $transport_type . " " . $transport_id . " ";
        echo "from " . $transport_details['from'] . " to " . $transport_details['to'] . ". ";

        echo "Seating is ";
        if(array_key_exists('seat', $transport_details)) { // if seat exists, display it
            echo "Seat " . $transport_details['seat'] . ". ";
        } else {
            echo "not assigned. ";
        }

        echo "Gate is ";
        if(array_key_exists('gate', $transport_details)) { // if gate exists, display it
            echo $transport_details['gate'] . ". ";
        } else {
            echo "not assigned. ";
        }

        if(array_key_exists('description', $transport_details)) { // if description exists, display it
            echo $transport_details['description'] . ". ";
        } 
        echo  "\n";
        $count = $count + 1;
    }
}

输出:

1. Take Airportbus NO from Barcelona to Gerona. Seating is not assigned. Gate is not assigned. 
2. Take flight SK455 from Gerona to Stockholm. Seating is Seat 3A. Gate is not assigned. 
3. Take flight SK22 from Stockholm to New york. Seating is Seat 7B. Gate is Gate 22. Baggage wiil be transfered from your last leg. 
4. Take train 78A from Madrid to Barcelona. Seating is Seat 45B. Gate is not assigned.  

【讨论】:

    【解决方案3】:

    我的程序解决方案没有验证或 cmets:

    <?php
    
    function printDefaultTransportation($properties) {
        $type = $properties['type'];
        $from = $properties['from'];
        $to   = $properties['to'];
        $id   = isset($properties['id']) ? $properties['id'] : null;
        $seat = isset($properties['seat']) ? $properties['seat'] : null;
    
        echo "Take $type";
        if( $id !== null ) {
            echo " $id";
        }
        echo " from $from to $to.";
        echo isset($seat) ? " Sit in $seat." : ' No seat assignment.';
    }
    
    function printAirportTransportation($properties) {
        $id   = $properties['id'];
        $from = $properties['from'];
        $to   = $properties['to'];
        $seat = isset($properties['seat']) ? $properties['seat'] : null;
        $gate = isset($properties['gate']) ? $properties['gate'] : null;
        $description = isset($properties['description']) ? $properties['description'] : null;
    
        echo "From $from Airport, take flight $id to $to.";
        if( $gate !== null ) {
            echo " $gate";
            if( $seat !== null ) {
                echo ", seat $seat";
            }
            echo '.';
        } else if( $seat !== null ) {
            echo " Seat $seat.";
        }
        if( $description !== null ) {
            echo " $description.";
        }
    }
    
    function printTransportation($type, $id, $extra) {
        static $functionsTypesMap = ['train'      => 'printDefaultTransportation',
                                     'airportbus' => 'printDefaultTransportation',
                                     'flight'     => 'printAirportTransportation'];
    
        $properties = array_merge(['type' => $type, 'id' => $id], $extra);
        call_user_func($functionsTypesMap[$type], $properties);
    }
    
    function printTransportations(array $transportations) {
        foreach($transportations as $k => $v) {
            $transportations[strtolower($k)] = $v;
        }
    
        $i = 1;
        static $orderedTypes = ['train', 'airportbus', 'flight'];
        foreach($orderedTypes as $type) {
            if( !isset($transportations[$type]) ) {
                continue;
            }
    
            foreach($transportations[$type] as $id => $properties) {
                if( $i > 1 ) {
                    echo "\n";
                }
                echo "$i. ";
                printTransportation($type,
                                    (strtolower($id)==='no'? null : $id),
                                    $properties);
                ++$i;
            }
        }
    }
    
    $arr = ["Airportbus" => ["NO" => ["from" => "Barcelona",
                                      "to"   => "Gerona"]],
            "flight"     => ["SK455" => ["from" => "Gerona",
                                         "to"   => "Stockholm",
                                         "seat" => "3A"],
                             "SK22"  => ["from" => "Stockholm",
                                         "to"   => "New york",
                                         "gate" => "Gate 22",
                                         "description" => "Baggage wiil be transfered from your last leg",
                                         "seat" => "7B"]],
            "train"      => ["78A" => ["from" => "Madrid",
                                       "to"   => "Barcelona",
                                       "seat" => "45B"]]
    ];
    
    printTransportations($arr);
    

    输出是:

    1.从马德里乘坐78A火车到巴塞罗那。坐在45B。 2. 从巴塞罗那乘坐机场巴士前往赫罗纳。没有座位分配。 3. 从赫罗纳机场乘坐 SK455 航班前往斯德哥尔摩。座位 3A。 4. 从斯德哥尔摩机场乘坐 SK22 航班前往纽约。 22 号登机口,座位 7B。行李将从您的最后一站转移。

    评论:

    • “flight SK455”缺少门,因为输入没有它。
    • “New York”输出为“New york”(来自数组值)。
    • 我认为,您应该使用类,而不是使用数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多