如何使用for循环比较前一个数组值与下一个数组值


How to compare the previous array value withe the next one using for loop

我想使用for循环从数组中匹配最后一个$data[$ I]和下一个$data[$ I]。我怎么能那样做呢?

下面是代码…

while($row=mysql_fetch_array($resultQuery))
    {
     echo "<br> Hotel Code : " .$row['HotelCode'];
     $info=(str_replace('YES ','',htmlspecialchars_decode($row['PAmenities'])));
     $str_replace='NO ';
     //$info=(str_replace('NO ','',htmlspecialchars_decode($row['PAmenities'])));
     $info_new=array();
     $info_new =(array_unique(explode(";",$info)));
     echo "<br> Hotel Features : " ;
     $string =implode(",",$info_new);
     $string = (str_replace('&apos,',"'",htmlspecialchars_decode($string)));
     //echo $string;
     $data=array();
     $data=(explode(",",$string));
     for($i = 0; $i < count($data); $i++)
     {

            echo (str_replace('NO ','',$data[$i])).",";
     }
     echo "<hr>";
输出:

Double bed,Bathroom,Shower,Bathtub,Hairdryer,Terrace,Balcony,Safe,Central heating,Individually adjustable heating,TV,Individually adjustable air conditioning,Direct dial telephone,Small pets allowed under 5 kg,Small pets allowed under 5 kg,Large pets allowed over 5 kg,Large pets allowed over 5 kg,**Wheelchair-accessible,Wheelchair-accessible,**Car park,Car park,Garage,24-hour reception,Wi-fi,Laundry service,Launderette,Lift access,Bar,Restaurant,Highchairs,TV lounge,Year of construction - 1970,Year of most recent renovation - 2005,Number of floors main building - 5,Total number of rooms - 37,Single rooms - 4,Double rooms - 27,Suites - 2,Connecting rooms,Connecting rooms,Hotel,Breakfast buffet,A la carte lunch,Set menu dinner,Nearest Bus / Metro Stop - 5000 m,

尝试:

for($i = 0; $i < count($data); $i++)
     {
      if($data[$i] == $lastdata)
      {
          // do stuff.
      }   
            echo (str_replace('NO ','',$data[$i]));
            $lastdata = $data[$i]
     }

如果你不想在循环中比较前一个值,总是将它存储在循环结束时的一个变量中。

在设置$lastdata中的值之前进行检查,并且每次都应该能够检查之前的值。