php - 显示该数组结果的最佳方式是什么

标签 php arrays string algorithm if-statement

我有一个多维数组

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 效率低下,因为每次添加新的数组键时我都必须添加更多代码。

遇到这种情况有没有更好的办法,请帮忙。预先感谢:)

最佳答案

我希望这张皱褶草图能让你有所收获

$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;

}

关于php - 显示该数组结果的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26029876/

相关文章:

php - 使用 Eloquent 保护数据透视表免受质量分配

python - Numpy 数组变量避免错误计算

javascript - 从 http 响应中提取 json 数组

python - 用大数组中的值 c 替换从值 a 到 b 的 float

php - 如何使用 PHP MySQL 在 AJAX 脚本中获取数据库值

PHP 重命名文件(如果存在)

php - mysql查询根据另一列的值按不同列排序

java - 在抛出 ArrayIndexOutOfBounds 异常之前检查 String[index] 是否为空

python - 检查字符串列表是否被准确找到 n 次

c - 当我将指针递增 1 时,为什么不能访问字符串中的下一个元素?