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

时间:2017-08-08 16:16:20

标签: php arrays

我有一个数组数组,其中包含带有值的数组键,例如:

ifconfig

如果我找到" Hotel"的关键字,我想要输出。然后我将从" Hotel"打印下一把钥匙。例如:

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. ) )

Hotel Jayakarta Jaya

这就是我现在正在做的事情:

Hotel menginap disana

输出:

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

预期输出应如下:

Hotel Jayakarta jaya sangat , Hotel Hotel menginap

非常感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:0)

也许你可以试试这个?

$arrayOfHotels = array(0 => array('Hotel' => 'N' , 'Jakarta' => '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.'));

echo '<pre>';
print_r(findHotel($arrayOfHotels));
echo '</pre>';

function findHotel($hotels)
{
    $isHotelFind = false;
    $isFirstThrough = true;
    foreach($hotels as $row => $value)
    {
        foreach ($value as $row2 => $value2)
        {
            if($row2 == "Hotel" && $value = 'N' && !$isHotelFind)
            {
                if($isFirstThrough)
                {
                    echo $row2 . " ";

                }
                else
                {
                    echo ", " . $row2 . " ";
                }
                $isHotelFind = true;
                $isFirstThrough = false;
            }
            if($isHotelFind && $row2 != "Hotel")
            {
                echo $row2 . " ";
            }
        }
        $isHotelFind = false;
    }
}

答案 1 :(得分:0)

执行以下操作:

$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 :(得分:0)

您可以使用array_search查找密钥并删除前面的前导元素。

<style>body {color: #000;}</style>
<del>&nbsp;&nbsp;<span style="color:#999">facebook</span>&nbsp;&nbsp;</del>

输出将如下:

$array = [
    [
     "Hotel"     => 'N',
     "Jayakarta" => 'NE',
     'Jaya'      => 'NE',
     'sangat'    => 'ADV',
     'nyaman'    => 'ADJ',
     'ditempati' => 'V.'
    ]
    ,
    [
     "Andi"     => 'NOB',
     "Hotel"    => 'N',
     'menginap' => 'V',
     'disana'   => 'N',
     'selama'   => 'N',
     'satu'     => 'NUM',
     'minggu'   => 'N.'
    ]
];


foreach ($array as $data) {
    // Find index to start
    $index = array_search("Hotel", array_keys($data));
    // Remove other keys
    $new_array_starting_hotel = array_splice($data, $index, count($data));
    // merge results
    $merged[] = implode(' ', array_keys($new_array_starting_hotel));
}

print_r($merged);

你可以随意合并或破坏所有。