doulangtiao4508 2017-08-08 16:16
浏览 14
已采纳

在数组数组中打印下一个数组键

I have an array of arrays that contain array key with the value, here the example :

Array  ( 
[0] => Array ( [Hotel] => N [Jayakarta] => NE [Jaya] => NE [sangat] => ADV [nyaman] => ADJ [ditempati] => V. )  
[1] => Array ( [Andi] => NOB [Hotel] => N [menginap] => V [disana] => N [selama] => N [satu] => NUM [minggu] => N. ) 
)

I want to make an output if I found a key of "Hotel" then I will print next key from "Hotel". For example :

Hotel Jayakarta Jaya

Hotel menginap disana

Here's what I am doing for the moment:

foreach($token2 as $index => $tok){
    foreach ($tok as $tokkey => $tokvalue) {
        if ($tokkey == "Hotel" and $tokvalue == "N"){
            echo $tokkey . " " ;

            while($cek == true && $x <= 2){
                $next = next($tok);
                echo key($tok). " " ;    

                $x++; 
            }
            echo ", ";
            $x = 1;
        }
    }   
}

And the output :

Hotel Jayakarta jaya sangat , Hotel Hotel menginap

The Expected Output should be as below:

Hotel Jayakarta jaya sangat , Hotel menginap disana selama

Any help is much appreciated, Thank you.

  • 写回答

3条回答 默认 最新

  • dtxq82489 2017-08-08 16:47
    关注

    Do something like following:

    $i = 3;
    foreach($token2 as $index => $tok){
        foreach ($tok as $tokkey => $tokvalue) {
            if ($tokkey == "Hotel" && $tokvalue == "N"){
                echo $tokkey . " " ;
                $i = 1;
            }
            else if($i < 3){
                echo $tokkey . " ";
                $i++;
            }
        }
        echo ", ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换