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

时间:2015-08-24 08:49:03

标签: php arrays

我想使用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,

1 个答案:

答案 0 :(得分:0)

尝试:

for($i = 0; $i < count($data); $i++)
     {

      if($data[$i] == $lastdata)
      {
          // do stuff.
      }   

            echo (str_replace('NO ','',$data[$i]));
            $lastdata = $data[$i]

     }

如果您不想比较循环中的先前值,请始终将其存储在循环END的变量中。在$ lastdata中设置值之前检查,您应该能够每次检查前一个值。